CODE: [Copy to clipboard]
Dim o
Set o = new CMakeFile
Call o.start("test.vbs")
Set o = Nothing
' 代码执行器,可以整合包含以下个是的脚本
' #include "文件名"
Class CMakeFile
Private fso ' 文件系统对象
Private dic ' 字典对象
Private shell
Private stream
Property Get FILE_NOT_FOUND()
FILE_NOT_FOUND = vbObjectError + 513
End Property
Property Get CLASS_NAME()
CLASS_NAME = "CMakeFile"
End Property
Sub class_initialize()
Set fso = CreateObject("scripting.filesystemobject")
Set dic = CreateObject("scripting.dictionary")
Set shell = CreateObject("wscript.shell")
End Sub
Sub class_terminate()
Set fso = Nothing
Set dic = Nothing
Set shell = Nothing
End Sub
Public Sub Require(filename)
On Error Resume Next
' 判断是否已经打开过此文件
If dic.exists(filename) Then
Exit Sub
Else
Call dic.add(filename,"")
End If
filename = fso.getabsolutepathname(filename)
If fso.fileexists(filename) Then
Dim file,line,buffer,arr
Set file = fso.opentextfile(filename,1,true)
line = 0
While Not file.atendofstream
line = line + 1
buffer = file.readline
If instr(1,buffer,"#include",vbTextCompare) = 1 Then
arr = Split(buffer,"""")
Call Require(arr(1))
If Err.number<>0 Then
Call Err.raise(Err.number,Err.source,"抛出异常在 " & filename & " 的第" & line & "行." & vbcrlf & Err.description)
End If
Else
Call stream.writeline(buffer)
End If
Wend
Call file.close()
Set file = Nothing
Else
Call Err.raise(FILE_NOT_FOUND,CLASS_NAME,"没有找到文件 """ & filename & """")
End If
End Sub
Sub Start(filename)
Set stream = fso.opentextfile("output.vbs",2,True)
Call require(filename)
If Err.number<>0 Then
Call MsgBox("错误: #" & Err.number & "源:" & Err.source & vbcrlf & "描述:" & vbcrlf & Err.description)
Else
Call MsgBox("output.vbs 成功生成!")
End If
Call stream.close()
Set stream = Nothing
End Sub
End Class