使用VBS计算.c文件中#defines的数量

时间:2009-04-09 08:15:45

标签: scripting vbscript

我需要使用VBS计算C文件中的#define行数(其中3行)。请建议实现这一目标的最佳方法。

4 个答案:

答案 0 :(得分:1)

下面的脚本使用正则表达式来计算出现在行首的#define语句。在语句之前以及#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 :(得分:0)

这样的事情怎么样?只需将文件作为参数传递


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

答案 2 :(得分:0)

此代码应计算任意数量的输入文件中的所有#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

答案 3 :(得分:0)

我对VB脚本并不擅长,但有点像......

Dim re as New RegExp
re.IgnoreCase = True
re.Global = True
re.Pattern = "#define"
Set mc = re.Execute(yourFileText)
ans = mc.Count