jmargel2
asked on
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
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:
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
jmargel2, you have posted this question twice. You should delete the other one before anyone posts a comment to it.
' 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).FirstLin e = 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
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).FirstLin
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), "-------------------------
blocks(numblocks).LastLine
phase = 0
End If
End If
Next
If phase = 2 Then ' end of block is missing
blocks(numblocks).LastLine
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
ASKER
I was able to extract this code & add to it, to make it work
Thanks all!!!!!!!!!!!
Thanks all!!!!!!!!!!!
Open "C:\my documents\testparse.txt" For Input As #1
Open "C:\my documents\newTestParse.txt
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)