此代码应计算任意数量的输入文件中的所有#define 语句。语句中已对空格进行了规定,例如“#define”或“#define”,它们都是有效的语句。
注意:我不计算一行的 # 和下一行的“define”,我假设 # 和“define”至少在同一行,但您可以通过阅读整个文件并删除所有空格,然后保存到变量或临时文件或类似的文件中,并将其用作您的输入。
你可以通过放弃文件访问常量来缩短代码,否则它会给你这样的输出:
There are: 9 define statements in the source: c:\define.txt
There are: 11 define statements in the source: c:\define2.txt
There are: 10 define statements in the source: c:\define3.txt
命令行应该是:cscript scriptname.vbs c:\define.txt c:\define3.txt etc...
' Define constants for file access
Const TristateFalse = 0
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
If objArgs.Count > 0 then
For i = 0 To objArgs.Count - 1
Set objFileInputStream = objFSO.OpenTextFile(objArgs(i), ForReading, False, TristateFalse)
intTempCount = 0
tokenCount = 0
Do while not objFileInputStream.AtEndOfStream
strLine = replace(ucase(objFileInputStream.ReadLine), " ", "")
intLineLength = len(strLine)
Do
If instr(strLine, "#DEFINE") <> 0 then
tokenCount = tokenCount + 1
strLine = replace(strLine, "#DEFINE","", 1, 1)
intTempCount = intTempCount + 1
else
exit do
End If
Loop until intTempCount = intLineLength
Loop
objFileInputStream.Close
wscript.echo "There are: " & tokenCount & " define statements in the source: " & objArgs(i)
Next
Else
wscript.echo "You must enter at least one filename."
End If