Solved

Parsing a text file

Posted on 2001-06-08
5
153 Views
Last Modified: 2010-05-02
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
Comment
Question by:jmargel2
5 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 6168132
   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
 
LVL 43

Accepted Solution

by:
TimCottee earned 200 total points
ID: 6168146
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
 
LVL 43

Expert Comment

by:TimCottee
ID: 6168195
jmargel2, you have posted this question twice. You should delete the other one before anyone posts a comment to it.
0
 
LVL 15

Expert Comment

by:ameba
ID: 6168454
' 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
 

Author Comment

by:jmargel2
ID: 6171437
I was able to extract this code & add to it, to make it work

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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now