0

如何使用VB6从包含错误关键字的文本文件中提取行。我对 VB6 很陌生,想从日志文件中提取多行并将它们放在日志文件的最后。

日志文件:Log.txt

2012-10-29 22:39:47 -------------------------- --

2012-10-29 22:39:47 申请开始...

2012-10-29 22:39:47 删除标志文件...

2012-10-29 22:39:47 成功认证为用户“管理员”

2012-10-29 22:40:11 显示角色选择对话框...

2012-10-29 22:40:13 角色选择:TESTROLE

2012-10-29 22:40:16 检查安装状态...

2012-10-29 22:41:21 成功连接BO服务器

2012-10-29 22:41:21 流程完成

2012-10-30 20:19:35 ----------------------------------------- --

2012-10-30 20:19:35 申请开始...

2012-10-30 20:19:35 删除标志文件...

2012-10-29 22:40:11 连接服务器时出错

2012-10-29 22:40:16 检查安装状态...

2012-10-29 22:41:21 ShellAndWait 函数出错。错误代码:0

2012-10-29 22:41:21 错误:2012-10-29 22:41:21 错误:对象“~”的方法“~”失败

2012-10-29 22:41:21 正在搜索本地源文件夹...

2012-10-29 22:41:21 错误:对象“~”的方法“~”失败

2012-10-29 22:41:21 复制文件夹\xxx.xxx.x.xx\xyz\Script 到 E:\

2012-10-29 22:41:21 非严重错误:复制文件夹时出错 \xxx.xxx.x.xx\admin 方法

2012-10-29 22:41:21 流程完成

在上面的日志文件中,我想在当前会话的句子中提取包含 Error 关键字的行,并希望收集所有这些行并将它们放在当前日志的最后。例如,在上面的日志文件中,当前日志在此会话中启动

"2012-10-30 20:19:35 ---------------------------------------- ——”

并以线结束

“2012-10-29 22:41:21 流程完成”

所以我想在这两行之间提取包含 Error 关键字的行并将它们放在“2012-10-29 22:41:21 The process completed”行下

“2012-10-29 22:41:21 流程完成”

下面列出了在会话 2012-10-29 22:40:11 中遇到的错误

2012-10-29 22:40:11 连接服务器时出错

2012-10-29 22:41:21 ShellAndWait 函数出错。错误代码:0

2012-10-29 22:41:21 错误:

2012-10-29 22:41:21 错误:对象“~”的方法“~”失败

2012-10-29 22:41:21 错误:对象“~”的方法“~”失败

012-10-29 22:41:21 非严重错误:复制文件夹时出错 \xxx.xxx.x.xx\admin 对象“~”的方法“~”

VB6可以吗?请指导我并提前感谢。

4

2 回答 2

3

普通的旧原生 VB6 I/O 可以很好地处理这样的事情。即使针对 80MB 的测试日志性能进行测试也是可以接受的。奖励:您不会以这种方式吃掉大量的 RAM。

作为奖励,这种尝试(a.)我认为它可以满足您的要求,(b.)进行不区分大小写的搜索,并且(c.)当序列“错误”发生在 a更长的词(例如“恐怖”)。

它还为没有“错误”的会话添加了“”行,但您可以根据需要更改或删除它,我将您的“遇到的错误...列在下面”消息调整为不那么笨重的内容。

Option Explicit

Private fOrigLog As Integer
Private fNewLog As Integer
Private colErrorLines As Collection
Private strSessionTimestamp As String

Private Sub FlushErrorLines()
    Print #fNewLog, "Errors encountered in session "; _
                    strSessionTimestamp; _
                    " are listed below"
    With colErrorLines
        If .Count = 0 Then
            Print #fNewLog, "*none*"
        Else
            Do While .Count > 0
                Print #fNewLog, .Item(1)
                .Remove 1
            Loop
        End If
    End With
End Sub

Private Sub Main()
    Const DASHES As String = "------------------------------------------"
    Dim strLine As String
    Dim strLineUCased As String
    Dim posError As Long

    fOrigLog = FreeFile(0)
    Open "log.txt" For Input As #fOrigLog Len = 32767

    fNewLog = FreeFile(0)
    Open "newlog.txt" For Output As #fNewLog Len = 32767

    Set colErrorLines = New Collection

    Do Until EOF(fOrigLog)
        Line Input #fOrigLog, strLine
        If Mid$(strLine, 21, 42) = DASHES Then
            If Len(strSessionTimestamp) > 0 Then
                FlushErrorLines
            End If
            strSessionTimestamp = Left$(strLine, 19)
            Print #fNewLog, strLine
        Else
            strLineUCased = UCase$(strLine)
            posError = InStr(21, strLineUCased, "ERROR")
            If posError > 0 Then
                'Make sure we have a whole word "ERROR" here:
                If Mid$(strLineUCased, posError - 1, 7) & " " _
                        Like "[!0-9A-Z]ERROR[!0-9A-Z]*" Then
                    colErrorLines.Add strLine
                Else
                    Print #fNewLog, strLine
                End If
            Else
                Print #fNewLog, strLine
            End If
        End If
    Loop
    Close #fOrigLog
    FlushErrorLines
    Close #fNewLog

    MsgBox "Complete"
End Sub
于 2013-06-14T22:29:18.507 回答
1

一些快速示例代码:

Private Sub Command1_Click()
  Dim lngLine As Long
  Dim strFile As String
  Dim intFile As Integer
  Dim strData As String
  Dim strLine() As String
  Dim strErrors As String
  'set filename
  strFile = "c:\temp\test.log"
  intFile = FreeFile
  'openj file
  Open strFile For Input As #intFile
    'read data from file
    strData = Input(LOF(intFile), #intFile)
  Close #intFile
  'split data into an array of lines
  strLine = Split(strData, vbCrLf)
  'empty error list
  strErrors = ""
  'loop through all lines of data
  For lngLine = 0 To UBound(strLine)
    If InStr(strLine(lngLine), "Error") > 0 Then
      'add line to errors if it contains the word "Error"
      strErrors = strErrors & strLine(lngLine) & vbCrLf
    End If
  Next lngLine
  'show the errors
  Print strErrors
End Sub
于 2013-06-13T13:35:40.133 回答