views:

307

answers:

4

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.

A: 

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


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
Tester101
Notice that the `#` and `define` tokens may be separated by an arbitrary number of whitespaces! `# define` is still a valid define-macro and isn't found by your code.
Konrad Rudolph
A: 

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.

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.

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...

' 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
unrealtrip
A: 

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

Dim re as New RegExp
re.IgnoreCase = True
re.Global = True
re.Pattern = "#define"
Set mc = re.Execute(yourFileText)
ans = mc.Count
JP Alioto
+1  A: 

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.:

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
Helen