我需要使用VBS計算C文件(其中3個)中#define
行的數量。請建議完成此操作的最佳方法。使用VBS計算.c文件中#defines的數量
2
A
回答
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
相關問題
- 1. 使用C計算目錄中文件的數量C
- 2. 計算文件中字的數量
- 3. 計算文件中的行數 - C
- 4. 計算文件中的字符數C#
- 5. 如何計算使用C++的文件中的行數?
- 6. 如何使用sed計算文件中非空行的數量?
- 7. 使用talend計算輸入XML文件中標記的數量
- 8. 計算在C++頭文件中定義的方法數量
- 9. C++ - 計算文件中元音的數量
- 10. 如何使用C來計算文件中的字節數?
- 11. 在.vbs中使用文件路徑中的變量
- 12. 計算行的文件數量-l
- 13. 計算上傳文件的數量
- 14. 使用批處理來計算文件夾中特定文件的數量
- 15. #defines in objective-c
- 16. 使用計數函數創建計算的度量條件
- 17. 使用CMD計算文件中無頭文件的行數
- 18. Qt nmake使用DEFINES變量構建
- 19. 如何使用C#計算具有特定名稱的子文件夾中的文件數量?
- 20. 用grep或sed計算文件中的單詞數量?
- 21. 用PHP計算推文的數量
- 22. 使用grep計算大量文件中的所有字符串
- 23. C#計算機中的搜索文件
- 24. 替換#defines常量
- 25. 如何用C#中的文本文件中的數字進行數學計算?
- 26. 如何使用批處理文件(.bat)計算進程數量
- 27. 使用Lotus Notes公式計算視圖中文檔的數量
- 28. 使用Spark計算文本或列表中元素的數量
- 29. 如何在VBS中使用文件名
- 30. 使用C++計算CSV文件中列的總和
的文本文件由一系列字符組成。如果您沒有定義一個「字符串」結束的位置以及下一個字符串的起始位置,則答案僅爲「1」。你可以創建一個函數返回1. – 2009-04-09 08:43:31
hi daniel, 爲了使問題更清楚更好。我想寫一個vbs腳本文件,它可以計算出現在a.c,b.c和d.c中的#define字符串的數量。 – Maddy 2009-04-09 08:57:46
我改寫了這個問題。請讓我知道它是否準確。 – 2009-04-09 09:13:17