2009-04-09 46 views
2

我需要使用VBS計算C文件(其中3個)中#define行的數量。請建議完成此操作的最佳方法。使用VBS計算.c文件中#defines的數量

+0

的文本文件由一系列字符組成。如果您沒有定義一個「字符串」結束的位置以及下一個字符串的起始位置,則答案僅爲「1」。你可以創建一個函數返回1. – 2009-04-09 08:43:31

+0

hi daniel, 爲了使問題更清楚更好。我想寫一個vbs腳本文件,它可以計算出現在a.c,b.c和d.c中的#define字符串的數量。 – Maddy 2009-04-09 08:57:46

+0

我改寫了這個問題。請讓我知道它是否準確。 – 2009-04-09 09:13:17

回答

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 

這將忽略#之間的空格和定義


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 
+1

請注意,'#'和'define'標記可能被任意數量的空格分隔! '#define'仍然是一個有效的define-macro,並且不會被你的代碼找到。 – 2009-04-09 15:33:13

0

此代碼應算上任意數量的輸入文件的所有#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

我不是那種善於VB腳本,但像...

Dim re as New RegExp 
re.IgnoreCase = True 
re.Global = True 
re.Pattern = "#define" 
Set mc = re.Execute(yourFileText) 
ans = mc.Count 
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