• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 168
  • Last Modified:

Parsing a text file

I have a log file, in which i only need certain information.  I would like this information to be automatically taken out of the existing log file & put into a new log file "newlogfile" with the date added onto it as the filename.

Here is an example of the part of the log I need:

Log: Possessed PlayerPawn: TMale1 CTF-Face.TMale0
DevNet: Join succeeded: /UH/Randomtask
ScriptLog: #### -------------------------------- ####
ScriptLog: ####      HACKED CLIENT DETECTED      ####
ScriptLog: #### -------------------------------- ####
ScriptLog: #### - Player Name : /UH/Randomtask
ScriptLog: #### - Player IP   : xxx.xxx.xxx.xxx
ScriptLog: #### - Method      : Virus detected in system, protection activated: UTMenu 436 (Unknown Cheat)
ScriptLog: #### - Action      : Kicked
ScriptLog: #### - Auth. Code  : -1456695808
ScriptLog: #### - Date/Time: 6-1-2001 / 16:5:43
ScriptLog: #### -------------------------------- ####
NetComeGo: Close TcpipConnection0 06/01/01 16:05:44
DevNet: NotifyAcceptingConnection: Server MyLevel accept
NetComeGo: Open MyLevel 06/01/01 16:10:51 24.12.1.135:4296


I need everything between "####      HACKED CLIENT DETECTED      ####"  & the last "#### -------------------------------- ####"

There could be alot of these within one log file.  I was able to use the simple common controls to open the file, but i need assistance on ths parsing.

Thanks

Jeff
0
jmargel2
Asked:
jmargel2
1 Solution
 
bobbit31Commented:
   Dim curLine As String

    Open "C:\my documents\testparse.txt" For Input As #1
    Open "C:\my documents\newTestParse.txt" For Output As #2
   
    While Not EOF(1)
        Input #1, curLine
       
        If curLine = "ScriptLog: ####      HACKED CLIENT DETECTED      ####" Then
            Write #2, curLine
            Input #1, curLine
           
            While curLine <> "NetComeGo: Close TcpipConnection0 06/01/01 16:05:44"
                Write #2, curLine
                Input #1, curLine
            Wend
        End If
    Wend

    Close (2)
    Close (1)
0
 
TimCotteeCommented:
Dim strLine As String
Dim blnInExtract As Boolean
Open "MyLogFile" For Input As #1
Open "MyNewLogFile_" & Format(Now(),"yyyy-mm-dd") For Output As #2
Do
  Line Input #1,strLine
  If blnInExtract Then
    Print #2,strLine
  End If
  If strLine = "ScriptLog: #### -------------------------------- ####" And Not blnInExtract Then
    blnInExtract = True
  Else
    blnInExtract = False
  End If
Loop Until Eof(1)
Close #1
Close #2

This should extract only the sections contained within ScriptLog: #### -------------------------------- ####
lines to a file named "MyNewLogFile_2001-06-08" if it were done today.
0
 
TimCotteeCommented:
jmargel2, you have posted this question twice. You should delete the other one before anyone posts a comment to it.
0
 
amebaCommented:
' Form1 code
Option Explicit
Dim LogLines() As String

Private Type TP_BLOCK
    FirstLine As Long
    Date As Date
    LastLine As Long
End Type
Dim blocks() As TP_BLOCK
Dim numblocks As Long

Private Sub Form_Click()
    Dim i As Long, j As Long, phase As Integer
    Dim tmpFileName As String, ff As Integer
   
    ReadFile "c:\test.log"
   
    'Phases:
    ' find start of block
    ' find date
    ' find end of block
   
    phase = 0 ' find start of block
   
    For i = 0 To UBound(LogLines)
        If phase = 0 Then ' find start of block
            If InStr(LogLines(i), "HACKED CLIENT DETECTED") Then
                ' start new block
                numblocks = numblocks + 1
                ReDim Preserve blocks(1 To numblocks)
                blocks(numblocks).FirstLine = i
                phase = 1
            End If
       
        ElseIf phase = 1 Then ' find date
            If InStr(LogLines(i), "Date/Time:") Then
                ' extract date
                blocks(numblocks).Date = ExtractDate(LogLines(i))
                phase = 2
            End If
       
        ElseIf phase = 2 Then ' find end of block
            If InStr(LogLines(i), "--------------------------------") Then
                blocks(numblocks).LastLine = i
                phase = 0
            End If
        End If
    Next
   
    If phase = 2 Then ' end of block is missing
        blocks(numblocks).LastLine = UBound(LogLines)
    ElseIf phase = 1 Then ' unknown date
        numblocks = numblocks - 1
        ReDim Preserve blocks(1 To numblocks)
    End If

    ' write log blocks to file (APPEND MODE)
    For i = 1 To numblocks
        tmpFileName = "c:\" & Format(blocks(i).Date, "yyyymmdd") & ".Log"
        ff = FreeFile
        Open tmpFileName For Append As ff
       
        For j = blocks(i).FirstLine To blocks(i).LastLine
            Print #ff, LogLines(j)
        Next
       
        Close ff
        Shell "notepad.exe " & tmpFileName, vbNormalFocus
    Next
End Sub

' read log file
Private Sub ReadFile(ByVal FileName As String)
    Dim ff As Integer, sAll As String
    ff = FreeFile
    On Error GoTo EH
   
    Open FileName For Binary As ff
    sAll = Space(LOF(ff))  ' buffer to hold file contents
    Get #ff, , sAll
    Close ff
 
    ' get all lines which contain "ScriptLog: ####"
    LogLines = Filter(Split(sAll, vbCrLf), "ScriptLog: ####", True)
    Exit Sub
EH:
    MsgBox Err.Description
End Sub

' extract date from log line - ScriptLog: #### - Date/Time: 6-1-2001 / 16:5:43
Private Function ExtractDate(LogLine As String) As Date
    Dim pos1 As Long, pos2 As Long, tmp As String, dateparts() As String
    On Error Resume Next
   
    pos1 = InStr(LogLine, "Date/Time:") + 10
    pos2 = InStr(pos1, LogLine, "/")
    tmp = Trim(Mid(LogLine, pos1, pos2 - pos1))
    dateparts = Split(tmp, "-")
    ExtractDate = DateSerial(dateparts(2), dateparts(0), dateparts(1))
    If Not IsDate(ExtractDate) Then
        ExtractDate = Now
    End If
End Function
0
 
jmargel2Author Commented:
I was able to extract this code & add to it, to make it work

Thanks all!!!!!!!!!!!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now