【问题标题】:VBA Macro : catch a line with specific string and return itVBA 宏:用特定字符串捕获一行并返回它
【发布时间】:2018-08-05 17:58:12
【问题描述】:

我试图从模拟报告 (.log) 中捕获模拟结果。我想匹配“通过”和“失败”字符串并将匹配完成的行保存在我将用于的其他结果文件中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....

只想返回带有通过和失败字符串的行并将其写入另一个日志文件(sim_results)

【问题讨论】:

  • 如果没有输入 .log 文件的样本和对包含行的标准的描述,这可能无法回答。
  • 代码对我有用。我注意到的奇怪之处是InputFile = Dir(Application.ActiveWorkbook.Path &amp; "\" &amp; "*.log") InputFile = Application.ActiveWorkbook.Path &amp; "\" &amp; InputFile 这两条线有什么用?您只需获取目录中带有结束日志的第一个文件。那个对吗?
  • InputFile = Dir(Application.ActiveWorkbook.Path & "\" & "*.log") 仅返回文件名(基本名称)而不是路径,这就是我使用第二个获取的原因读取文件的整个文件名路径。您是否获得了内容正确的输出文件:失败和通过的行?
  • 是的,我只是失败并通过了行。你有一个“Unix”文件吗?

标签: vba excel fso


【解决方案1】:

使用Line Input 读取文件时,您必须考虑到 Line Input 仅在读取 CR 或 CR 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

【讨论】:

  • 你有 wright..它是一个 Unix 文件。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2019-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多