【问题标题】:Count the number of #defines in a .c file using VBS使用 VBS 计算 .c 文件中 #defines 的数量
【发布时间】:2010-10-18 12:18:14
【问题描述】:

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

【问题讨论】:

  • 一个文本文件由一系列字符组成。如果您没有定义一个“字符串”的结束位置和下一个“字符串”的开始位置,那么答案就是“1”。你可以创建一个只返回 1 的函数。
  • hi daniel,为了让问题更清晰和更好。我想编写一个 vbs 脚本文件来计算 a.c、b.c 和 d.c 中存在的#define 字符串的数量。
  • 我已经根据这个重写了这个问题。请让我知道它是否准确。
  • 我没有得到你,乔恩??你在哪里重写了我的问题。
  • 以下答案之一是否解决了您的问题?如果是这样,您应该将此问题标记为已回答。我注意到您有很多问题有答案,但没有一个被标记为已回答。

标签: scripting vbscript


【解决方案1】:

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


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 仍然是有效的定义宏,您的代码找不到。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    我不太擅长 VB 脚本,但类似...

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

    【讨论】:

      【解决方案4】:

      下面的脚本使用正则表达式来计算出现在行首的#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
      

      【讨论】:

        猜你喜欢
        • 2015-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-22
        • 2010-11-10
        • 2020-10-09
        • 2011-09-20
        相关资源
        最近更新 更多