[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Parsing a text file

Posted on 2001-06-08
5
Medium Priority
?
166 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 400 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

650 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