Scripting 使用VBS计算.c文件中的#定义数
我需要使用VBS计算C文件中的Scripting 使用VBS计算.c文件中的#定义数,scripting,vbscript,Scripting,Vbscript,我需要使用VBS计算C文件中的#define行数(其中3行)。请建议实现这一目标的最佳方法。像这样的方法怎么样?只需将文件作为参数传入即可 Const token = "#define" Set objArgs = WScript.Arguments tokenCount = 0 Set fso = CreateObject("Scripting.FileSystemObject") For i = 0 To objArgs.Count - 1 Set objFile = fso
#define
行数(其中3行)。请建议实现这一目标的最佳方法。像这样的方法怎么样?只需将文件作为参数传入即可
Const token = "#define"
Set objArgs = WScript.Arguments
tokenCount = 0
Set fso = CreateObject("Scripting.FileSystemObject")
For i = 0 To objArgs.Count - 1
Set objFile = fso.OpenTextFile(objArgs(i), 1)
Do Until objFile.AtEndofStream
lineCount = lineCount + 1
If InStr(1, objFile.ReadLine, token, 1) 0 Then
tokenCount = tokenCount + 1
End If
Loop
Next
WScript.echo tokenCount
这将忽略#和define之间的空格
Const token = "#define"
Set objArgs = WScript.Arguments
tokenCount = 0
Set fso = CreateObject("Scripting.FileSystemObject")
For i = 0 To objArgs.Count - 1
Set objFile = fso.OpenTextFile(objArgs(i), 1)
Do Until objFile.AtEndofStream
lineCount = lineCount + 1
If InStr(1, Replace(objFile.ReadLine, " ", "", 1, -1, 1), token, 1) 0 Then
tokenCount = tokenCount + 1
End If
Loop
Next
WScript.echo tokenCount
此代码应统计任意数量输入文件中的所有#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等
' 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
定义文件访问的常量
常数三态假=0
常数ForReading=1
写入常数=2
出现的常数=8
设置objFSO=CreateObject(“Scripting.FileSystemObject”)
Set objArgs=WScript.Arguments
如果objArgs.Count>0,则
对于i=0到objArgs.Count-1
设置objFileInputStream=objFSO.OpenTextFile(objArgs(i)、ForReading、False、tristateflse)
intTempCount=0
tokenCount=0
不执行objFileInputStream.AtEndOfStream
strLine=replace(ucase(objFileInputStream.ReadLine),“”,“”)
intLineLength=len(strLine)
做
如果instr(strLine,“#DEFINE”)为0,则
tokenCount=tokenCount+1
strLine=replace(strLine,#DEFINE,“”,1,1)
intTempCount=intTempCount+1
其他的
退出do
如果结束
循环直到intTempCount=intLineLength
环
objFileInputStream.Close
wscript.echo“源:”&objArgs(i)中有:”&tokenCount&“define语句”
下一个
其他的
wscript.echo“您必须输入至少一个文件名。”
如果结束
我不太擅长VB脚本,但有点像
Dim re as New RegExp
re.IgnoreCase = True
re.Global = True
re.Pattern = "#define"
Set mc = re.Execute(yourFileText)
ans = mc.Count
下面的脚本使用正则表达式计算出现在行首的
#define
语句。语句之前以及
和定义之间允许有空格。要搜索的文件列表应作为参数传入,例如:
cscript.exe script_name.vbs c:\myfile1.c c:\myfile2.c
以下是脚本代码:
Const ForReading = 1
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set re = New RegExp
re.Pattern = "^\s*#\s*define\b"
re.IgnoreCase = False
re.Global = True
re.Multiline = True
strReport = ""
For Each strFileName in WScript.Arguments.Unnamed
Set oFile = oFSO.OpenTextFile(strFileName, ForReading)
strText = oFile.ReadAll
oFile.Close
intCount = re.Execute(strText).Count
strReport = strReport & "There are " & intCount & " #define statement(s) in " & strFileName & vbNewLine
Next
WScript.Echo strReport
脚本输出如下所示:
There are 7 #define statement(s) in c:\myfile1.c
There are 0 #define statement(s) in c:\myfile2.c
文本文件由一系列字符组成。如果不定义一个“字符串”的结束位置和下一个“字符串”的开始位置,那么答案就是“1”。您可以创建一个只返回1.hi daniel的函数,以使问题更清楚、更好。我想编写一个vbs脚本文件,计算a.c、b.c和d.c中存在的#define字符串的数量。我已基于此重写了问题。请让我知道它是否准确。我没有得到你乔恩??你在哪里重写了我的问题。下面的答案之一解决了你的问题吗?如果是这样,你应该把这个问题标记为已回答。我注意到有很多问题都有答案,但没有一个问题被标记为已回答。请注意,
和define
标记可以用任意数量的空格分隔#define
仍然是有效的define宏,代码找不到它。