您好,能用vba帮我写个可以检索出指定目录包括子目录里的java源文件里的方法名以及统计每个方法的有效代码行数,空行数,注释行数并生成一个excel的一览表的程序吗
时间: 2023-05-14 12:05:48 浏览: 89
你好,可以使用VBA编写一个程序来检索指定目录及其子目录中的Java源文件,并统计每个方法的有效代码行数、空行数和注释行数,最后生成一个Excel表格。以下是示例代码:
Sub SearchJavaFiles()
Dim folderPath As String
Dim fileExtension As String
Dim methodName As String
Dim codeLines As Long
Dim blankLines As Long
Dim commentLines As Long
Dim excelRow As Long
folderPath = "C:\JavaProject"
fileExtension = "*.java"
excelRow = 1
' Create Excel table headers
Range("A1").Value = "Method Name"
Range("B1").Value = "Code Lines"
Range("C1").Value = "Blank Lines"
Range("D1").Value = "Comment Lines"
' Search for Java files in the specified folder and its subfolders
Call SearchFolder(folderPath, fileExtension, methodName, codeLines, blankLines, commentLines, excelRow)
End Sub
Sub SearchFolder(ByVal folderPath As String, ByVal fileExtension As String, ByRef methodName As String, ByRef codeLines As Long, ByRef blankLines As Long, ByRef commentLines As Long, ByRef excelRow As Long)
Dim fileName As String
Dim filePath As String
Dim fileContent As String
Dim fileLines() As String
Dim line As String
Dim i As Long
Dim inComment As Boolean
' Search for files in the current folder
fileName = Dir(folderPath & "\" & fileExtension)
Do While fileName <> ""
filePath = folderPath & "\" & fileName
' Read file content
Open filePath For Input As #1
fileContent = Input$(LOF(1), 1)
Close #1
' Split file content into lines
fileLines = Split(fileContent, vbNewLine)
' Initialize variables
methodName = ""
codeLines = 0
blankLines = 0
commentLines = 0
inComment = False
' Process each line in the file
For i = 0 To UBound(fileLines)
line = Trim(fileLines(i))
' Check for method declaration
If Left(line, 4) = "public" And InStr(line, "(") > 0 And InStr(line, ")") > 0 Then
methodName = Mid(line, 8, InStr(line, "(") - 8)
End If
' Count code, blank, and comment lines
If Len(line) = 0 Then
blankLines = blankLines + 1
ElseIf Left(line, 2) = "//" Then
commentLines = commentLines + 1
ElseIf Left(line, 2) = "/*" Then
commentLines = commentLines + 1
inComment = True
ElseIf Right(line, 2) = "*/" Then
commentLines = commentLines + 1
inComment = False
ElseIf inComment Then
commentLines = commentLines + 1
Else
codeLines = codeLines + 1
End If
Next i
' Write results to Excel table
If methodName <> "" Then
excelRow = excelRow + 1
Range("A" & excelRow).Value = methodName
Range("B" & excelRow).Value = codeLines
Range("C" & excelRow).Value = blankLines
Range("D" & excelRow).Value = commentLines
End If
' Search for next file in the folder
fileName = Dir()
Loop
' Search for subfolders in the current folder
fileName = Dir(folderPath, vbDirectory)
Do While fileName <> ""
If fileName <> "." And fileName <> ".." Then
If (GetAttr(folderPath & "\" & fileName) And vbDirectory) = vbDirectory Then
Call SearchFolder(folderPath & "\" & fileName, fileExtension, methodName, codeLines, blankLines, commentLines, excelRow)
End If
End If
fileName = Dir()
Loop
End Sub
请注意,此代码仅供参考,您需要根据自己的需求进行修改和调整。
阅读全文