VBA 宏:捕获带有特定字符串的行并 return 它

VBA Macro : catch a line with specific string and return it

我正在尝试从模拟报告 (.log) 中捕获模拟结果。我想匹配 "passed" 和 "failed" 字符串并保存在其他结果中完成匹配的行我将在 excel 中用于进一步操作的文件。我无法匹配所需的行:我编写的以下子程序不仅写入了所需的行,还写入了所有行。不知道问题出在哪里..

Sub generate_sim_results()

    Dim testResults  As String
    Dim InputFile As String
    Dim fso As Object
    Dim ObjFile As Object
    Set fso = CreateObject("Scripting.FileSystemObject")


    testResults = Application.ActiveWorkbook.Path & "\sim_results.log"
    If Len(Dir$(testResults)) > 0 Then
        Kill testResults
    End If


    InputFile = Dir(Application.ActiveWorkbook.Path & "\" & "*.log")
    InputFile = Application.ActiveWorkbook.Path & "\" & InputFile
    'Debug.Print InputFile

    fnum = FreeFile()
    Open InputFile For Input As #fnum
    Set ObjFile = fso.CreateTextFile(testResults)

    While Not EOF(fnum)

        Line Input #fnum, dataLine
        If InStr(1, dataLine, "passed", vbTextCompare) > 0 Or InStr(1, dataLine,"failed", vbTextCompare) > 0 Then
            ObjFile.Writeline dataLine
            Debug.Print dataLine
        End If

    Wend

    ObjFile.Close
    Set fso = Nothing
    Set ObjFile = Nothing

End Sub

*sample of the log file*
<time="1500 ns" instance="testcase_00">passed
<time="342100480 ps" instance="testcase_01">passed
blabla informations about the tests....
<time="742894 ns" instance="testcase_02_01">passed
blabla informations about the tests....
blabla informations about the tests....
<time="744121040 ps" instance="testcase_02_02">failed
blabla informations about the tests....
<time="745034560 ps" instance="testcase_02_03">passed
blabla informations about the tests....
<time="745134560 ps" instance="testcase_02_04">passed
blabla informations about the tests....
blabla informations about the tests....
blabla informations about the tests....
blabla informations about the tests....
<time="745548080 ps" instance="testcase_03">failed
<time="747388640 ps" instance="testcase_04_01">passed
<time="750745100 ns" instance="testcase_04_02">passed
blabla informations about the tests....

只想return仅包含通过和失败字符串的行并将其写入另一个日志文件(sim_results)

当使用 Line Input 读取文件时,您必须考虑到行输入只会在读取 CR 或 CR and LF 时停止,但它会如果它只读取一个 LF,则不要停止。在您的情况下,您需要将 LF 转换为 CR 和 LF。你可以用 Notpad++

由于您已经在使用 FSO,因此您也可以像这样使用它来读取文件。那你就不用操心"Unix".

Sub generate_sim_results()

Dim testResults As String
Dim InputFile As String
Dim fso As Object
Dim ObjFile As Object
Dim fnum As Long
Dim dataLine As String
    Set fso = CreateObject("Scripting.FileSystemObject")


    testResults = Application.ActiveWorkbook.Path & "\sim_results.log"
    If Len(Dir$(testResults)) > 0 Then
        Kill testResults
    End If


    InputFile = Dir(Application.ActiveWorkbook.Path & "\" & "*.log")
    InputFile = Application.ActiveWorkbook.Path & "\" & InputFile
    'Debug.Print InputFile

    fnum = FreeFile()
    Open InputFile For Input As #fnum
    Set ObjFile = fso.CreateTextFile(testResults)

    Dim myFile
    Set myFile = fso.OpenTextFile(InputFile, 1)
    Do While myFile.AtEndOfStream <> True
        dataLine = myFile.ReadLine
        If InStr(1, dataLine, "passed", vbTextCompare) > 0 Or InStr(1, dataLine, "failed", vbTextCompare) > 0 Then
            ObjFile.Writeline dataLine
            Debug.Print dataLine
        End If

    Loop
    myFile.Close
    ObjFile.Close
    Set fso = Nothing
    Set ObjFile = Nothing

End Sub