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

时间:2023-01-09 07:29:24

I need to count the number of #define lines in C files (3 of them) using VBS. Please suggest the best way to accomplish this.

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

4 个解决方案

#1


The script below uses a regular expression to count the #define statements that appear at the beginning of the lines. Whitespace is allowed before the statement as well as between # and define. The list of files to search in should be passed in as arguments, e.g.:

下面的脚本使用正则表达式来计算出现在行开头的#define语句。在语句之前以及#和define之间允许空格。要搜索的文件列表应作为参数传递,例如:

cscript.exe script_name.vbs c:\myfile1.c c:\myfile2.c

Here's the script code:

这是脚本代码:

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

The script output is like:

脚本输出如下:

There are 7 #define statement(s) in c:\myfile1.c
There are 0 #define statement(s) in c:\myfile2.c

#2


How about something like this? just pass the files in as arguments

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


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

This will ignore blank spaces between # and define

这将忽略#和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

#3


This code should count all #define statements in any number of input files. Provisions have been made for whitespace in the statements, such as "# define" or "# define" both of which are valid statements.

此代码应计算任意数量的输入文件中的所有#define语句。已对语句中的空格进行了规定,例如“#define”或“#define”两者都是有效的语句。

NOTE: I do not count for the # on one line and the "define" on the next, I'm assuming the # and "define" are at least on the same line, though you could do so by reading the entire file and removing all whitespace, then save to a variable or temp file or something like that and use that as your input.

注意:我不计算一行上的#和下一行的“定义”,我假设#和“define”至少在同一行,尽管你可以通过阅读整个文件和删除所有空格,然后保存到变量或临时文件或类似的东西,并将其用作输入。

You can shorten the code a bunch by ditching the file access constants and what not, otherwise it will give you output like this:

您可以通过放弃文件访问常量来缩短代码,不然的是,否则它将为您提供如下输出:

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

The command line would be: cscript scriptname.vbs c:\define.txt c:\define3.txt etc...

命令行是: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

#4


I'm not that good at VB script, but something like ...

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

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

#1


The script below uses a regular expression to count the #define statements that appear at the beginning of the lines. Whitespace is allowed before the statement as well as between # and define. The list of files to search in should be passed in as arguments, e.g.:

下面的脚本使用正则表达式来计算出现在行开头的#define语句。在语句之前以及#和define之间允许空格。要搜索的文件列表应作为参数传递,例如:

cscript.exe script_name.vbs c:\myfile1.c c:\myfile2.c

Here's the script code:

这是脚本代码:

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

The script output is like:

脚本输出如下:

There are 7 #define statement(s) in c:\myfile1.c
There are 0 #define statement(s) in c:\myfile2.c

#2


How about something like this? just pass the files in as arguments

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


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

This will ignore blank spaces between # and define

这将忽略#和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

#3


This code should count all #define statements in any number of input files. Provisions have been made for whitespace in the statements, such as "# define" or "# define" both of which are valid statements.

此代码应计算任意数量的输入文件中的所有#define语句。已对语句中的空格进行了规定,例如“#define”或“#define”两者都是有效的语句。

NOTE: I do not count for the # on one line and the "define" on the next, I'm assuming the # and "define" are at least on the same line, though you could do so by reading the entire file and removing all whitespace, then save to a variable or temp file or something like that and use that as your input.

注意:我不计算一行上的#和下一行的“定义”,我假设#和“define”至少在同一行,尽管你可以通过阅读整个文件和删除所有空格,然后保存到变量或临时文件或类似的东西,并将其用作输入。

You can shorten the code a bunch by ditching the file access constants and what not, otherwise it will give you output like this:

您可以通过放弃文件访问常量来缩短代码,不然的是,否则它将为您提供如下输出:

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

The command line would be: cscript scriptname.vbs c:\define.txt c:\define3.txt etc...

命令行是: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

#4


I'm not that good at VB script, but something like ...

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

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