Sign in to follow this  
Followers 0
Waeolwen

Problem parsing text files... Help please :)

15 posts in this topic

#1 ·  Posted (edited)

I'm trying to make an app to parse through some log files and pull only the parts I want out. I'm trying to gather 2 specific types of lines from the logs, which I'm able to do by parsing them out to a separate .txt file for each line type (2 resulting files).

What I can't get to work is my final function which should read one of the resulting .txt's and copy the lines into the other text file. For some reason, it's not reading any of the lines in the file. But, if I copy just that section of code out, it does it perfectly. I'm thinking my variable (or whatever) that's storing the line number isn't clearing out so it starts at the end of that file instead the beginning again. I've tried closing the file and reopening it just for this function, but it still seems to fail out.

Is there a way to empty out all of your variables?

Or, does anyone see anything wrong here? :)

#NoTrayIcon
#include <GUIConstants.au3>
Global $start, $end, $dateS, $dateE, $log, $line, $monthS, $monthE, $dayS, $dayE, $hourS, $hourE, $hourstored, $daystored
Global $pmS, $pmE, $player, $loot2, $loots
$gui = GUICreate("Enter information", 145, 295)
GUISetState(@SW_SHOW)
GUICtrlCreateLabel("Enter player name:", 10, 20)
$playerC = GUICtrlCreateInput ("", 10, 35, 100, 20)
GUICtrlCreateLabel("Enter START date: (mm:dd)", 10, 60)
$monthC = GUICtrlCreateInput ("mm", 10, 75, 25, 20)
$dayC = GUICtrlCreateInput ("dd", 45, 75, 25, 20)
GUICtrlCreateLabel("Enter START time: (hh)", 10, 100)
$hourC = GUICtrlCreateInput ("hh", 10, 115, 25, 20)
$pmC = GUICtrlCreateCheckbox("PM", 80, 115)
GUICtrlCreateLabel("Enter END date: (mm:dd)", 10, 140)
$monthD = GUICtrlCreateInput ("mm", 10, 155, 25, 20)
$dayD = GUICtrlCreateInput ("dd", 45, 155, 25, 20)
GUICtrlCreateLabel("Enter END time: (hh)", 10, 180)
$hourD = GUICtrlCreateInput ("hh", 10, 195, 25, 20)
$pmD = GUICtrlCreateCheckbox("PM", 80, 195)
$startB = GUICtrlCreateButton("Start Parse", 20, 220)
$exit = GUICtrlCreateButton("Exit", 90, 220)
GUICtrlCreateLabel("MAKE SURE ALL NUMBERS", 2, 255)
GUICtrlCreateLabel("ARE 2 DIGITS LONG", 2, 270)
ControlFocus("Enter information", "", $playerC)

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $startB
            GetData()
            $hourstored = $hourS
            $daystored = $dayS
            CreateDate()
            FindStart1()
            ExitLoop
        Case $msg = $exit
            ExitLoop
    EndSelect
Wend

Func GetData()
    $player = GUICtrlRead($playerC)
    $monthS = GUICtrlRead($monthC)
    $dayS = GUICtrlRead($dayC)
    $hourS = GUICtrlRead($hourC)
    $pmS = GUICtrlRead($pmC)
    $monthE = GUICtrlRead($monthD)
    $dayE = GUICtrlRead($dayD)
    $hourE = GUICtrlRead($hourD)
    $pmE = GUICtrlRead($pmD)
    If $pmS = $GUI_CHECKED Then
        $hourS = $hourS + 12
    ElseIf $hourS = 12 Then
        $hourS = 0
    EndIf
    If $pmE = $GUI_CHECKED Then
        $hourE = $hourE + 12
    ElseIf $hourE = 12 Then
        $hourE = 0
    EndIf
    $line = ""
EndFunc

Func CreateDate()
    Select 
        Case $monthS = 1
            $monthS = "Jan"
        Case $monthS = 2
            $monthS = "Feb"
        Case $monthS = 3
            $monthS = "Mar"
        Case $monthS = 4
            $monthS = "Apr"
        Case $monthS = 5
            $monthS = "May"
        Case $monthS = 6
            $monthS = "Jun"
        Case $monthS = 7
            $monthS = "Jul"
        Case $monthS = 8
            $monthS = "Aug"
        Case $monthS = 9
            $monthS = "Sep"
        Case $monthS = 10
            $monthS = "Oct"
        Case $monthS = 11
            $monthS = "Nov"
        Case $monthS = 12
            $monthS = "Dec"
    EndSelect
    $dateS = $monthS & " " & $dayS
    $start = $dateS & " " & $hourS
    Select 
        Case $monthE = 1
            $monthE = "Jan"
        Case $monthE = 2
            $monthE = "Feb"
        Case $monthE = 3
            $monthE = "Mar"
        Case $monthE = 4
            $monthE = "Apr"
        Case $monthE = 5
            $monthE = "May"
        Case $monthE = 6
            $monthE = "Jun"
        Case $monthE = 7
            $monthE = "Jul"
        Case $monthE = 8
            $monthE = "Aug"
        Case $monthE = 9
            $monthE = "Sep"
        Case $monthE = 10
            $monthE = "Oct"
        Case $monthE = 11
            $monthE = "Nov"
        Case $monthE = 12
            $monthE = "Dec"
    EndSelect
    $dateE = $monthE & " " & $dayE
    $end = $dateE & " " & $hourE
EndFunc

Func FindStart1()
    $log = FileOpen("C:\Program Files\Everquest\Logs\eqlog_" & $player & "_erollisi.txt", 0)
    While 1
        $line = FileReadLine($log)
        If @error = -1 Then HourInc1()
        If StringInStr($line, $start) <> 0 Then
            OpenFileWhoAll()
        EndIf
    Wend
EndFunc

Func OpenFileWhoAll()
    $loots = FileOpen("C:\Program Files\Everquest\Logs\" & $monthS & "_" & $daystored & ".txt", 2)
    $loot2 = FileOpen("C:\Program Files\Everquest\Logs\TempLoots_" & $monthS & "_" & $daystored & ".txt", 2)
    FileWriteLine($loots,"~~~> STARTING LOG PARSE: " & $monthS &  " " & $dayS & " " & $hourS & ":00")
    While 1
        $line = FileReadLine($log)
        If @error = -1 Then ExitLoop
        If StringInStr($line, $end) <> 0 Then
            ExitLoop
        EndIf
        Select
            Case StringInStr($line, "<Triune>") <> 0
                If StringInStr($line, "[ANONYMOUS]") = 0 Then
                    FileWriteLine($loots, $line)
                EndIf
            Case StringInStr($line, "tells triloot") <> 0
                If StringInStr($line, "grat") <> 0 Then
                    FileWriteLine($loot2, $line)
                EndIf
        EndSelect
    Wend
EndFunc

Func HourInc1()
    $hourS = $hourS + 1
    If $hourS = 24 and $pmS = $GUI_CHECKED Then
        $hourS = "00"
        $dayS = $dayS + 1
            If StringLen($dayS) < 2 Then
                $dayS = "0" & $dayS
            EndIf
    ElseIf $hourS = $hourE Then
        MoveLoots()
        Exit
    EndIf

    $dateS = $monthS & " " & $dayS
    $start = $dateS & " " & $hourS
    FileClose($log)
    FindStart1()
EndFunc

Func MoveLoots()
    MsgBox(1,"","moving loots")
    $loot2 = FileOpen("C:\Program Files\Everquest\Logs\TempLoots_" & $monthS & "_" & $daystored & ".txt", 0)
    $loots = FileOpen("C:\Program Files\Everquest\Logs\" & $monthS & "_" & $daystored & ".txt", 1)
    While 1
        $line = FileReadLine($loot2)
        If @error = -1 Then ExitLoop
        FileWriteLine($loots, $line)
    Wend
EndFunc
Edited by Waeolwen

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You never close the file $loot2 in the OpenFileWhoAll() function, therefore when you try to open the same file in the last function the opening will fail and your global variable $loot2 will hold -1 and that my friend, is not a valid filehandle to use in FileReadLine (also the filehandle in $loot2 was in write mode so it wouldn't work anyway).

The solution is simple though, just Add a FileClose($loot2) at the end of OpenFileWhoAll() or at the start of the last function :)

Edited by monoceres

Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

You never close the file $loot2 in the OpenFileWhoAll() function, therefore when you try to open the same file in the last function the opening will fail and your global variable $loot2 will hold -1 and that my friend, is not a valid filehandle to use in FileReadLine (also the filehandle in $loot2 was in write mode so it wouldn't work anyway).

The solution is simple though, just Add a FileClose($loot2) at the end of OpenFileWhoAll() or at the start of the last function :)

Even changing the last function to:

Func MoveLoots()
    FileClose($loots)
    FileClose($loot2)
    FileClose($log)
    $loot2 = FileOpen("C:\Program Files\Everquest\Logs\TempLoots_" & $monthS & "_" & $daystored & ".txt", 0)
    $loots = FileOpen("C:\Program Files\Everquest\Logs\" & $monthS & "_" & $daystored & ".txt", 1)
    While 1
        $line2 = FileReadLine($loot2)
        If @error = -1 Then ExitLoop
        MsgBox(1,"",$line2)
        FileWriteLine($loots, $line2)
    Wend
EndFunc

Does the same thing. It writes the two text files, but fails to complete this function. It reaches '@error = -1' (end of file check) on the first line read for some reason.

I know it reaches this function (that was the purpose the MsgBox in my first paste), but it stops on the first line read. Seems to me that even if I close the file and reopen it, it's starting at the last line for some reason :)

Edited by Waeolwen

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

#NoTrayIcon
#include <GUIConstants.au3>
#include <file.au3>
Global $start, $end, $dateS, $dateE, $log, $line, $monthS, $monthE, $dayS, $dayE, $hourS, $hourE, $hourstored, $daystored
Global $pmS, $pmE, $player, $loot2, $loots
Global $ePath = "C:\Program Files\Everquest\Logs\"
$gui = GUICreate("Enter information", 145, 295)
GUISetState(@SW_SHOW)
GUICtrlCreateLabel("Enter player name:", 10, 20)
$playerC = GUICtrlCreateInput ("", 10, 35, 100, 20)
GUICtrlCreateLabel("Enter START date: (mm:dd)", 10, 60)
$monthC = GUICtrlCreateInput ("mm", 10, 75, 25, 20)
$dayC = GUICtrlCreateInput ("dd", 45, 75, 25, 20)
GUICtrlCreateLabel("Enter START time: (hh)", 10, 100)
$hourC = GUICtrlCreateInput ("hh", 10, 115, 25, 20)
$pmC = GUICtrlCreateCheckbox("PM", 80, 115)
GUICtrlCreateLabel("Enter END date: (mm:dd)", 10, 140)
$monthD = GUICtrlCreateInput ("mm", 10, 155, 25, 20)
$dayD = GUICtrlCreateInput ("dd", 45, 155, 25, 20)
GUICtrlCreateLabel("Enter END time: (hh)", 10, 180)
$hourD = GUICtrlCreateInput ("hh", 10, 195, 25, 20)
$pmD = GUICtrlCreateCheckbox("PM", 80, 195)
$startB = GUICtrlCreateButton("Start Parse", 20, 220)
$exit = GUICtrlCreateButton("Exit", 90, 220)
GUICtrlCreateLabel("MAKE SURE ALL NUMBERS", 2, 255)
GUICtrlCreateLabel("ARE 2 DIGITS LONG", 2, 270)
ControlFocus("Enter information", "", $playerC)

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $startB
            GetData()
            $hourstored = $hourS
            $daystored = $dayS
            CreateDate()
            FindStart1()
            ExitLoop
        Case $msg = $exit
            ExitLoop
    EndSelect
Wend

Func GetData()
    $player = GUICtrlRead($playerC)
    $monthS = GUICtrlRead($monthC)
    $dayS = GUICtrlRead($dayC)
    $hourS = GUICtrlRead($hourC)
    $pmS = GUICtrlRead($pmC)
    $monthE = GUICtrlRead($monthD)
    $dayE = GUICtrlRead($dayD)
    $hourE = GUICtrlRead($hourD)
    $pmE = GUICtrlRead($pmD)
    If $pmS = $GUI_CHECKED Then
        $hourS = $hourS + 12
    ElseIf $hourS = 12 Then
        $hourS = 0
    EndIf
    If $pmE = $GUI_CHECKED Then
        $hourE = $hourE + 12
    ElseIf $hourE = 12 Then
        $hourE = 0
    EndIf
    $line = ""
EndFunc

Func CreateDate()
    Select
        Case $monthS = 1
            $monthS = "Jan"
        Case $monthS = 2
            $monthS = "Feb"
        Case $monthS = 3
            $monthS = "Mar"
        Case $monthS = 4
            $monthS = "Apr"
        Case $monthS = 5
            $monthS = "May"
        Case $monthS = 6
            $monthS = "Jun"
        Case $monthS = 7
            $monthS = "Jul"
        Case $monthS = 8
            $monthS = "Aug"
        Case $monthS = 9
            $monthS = "Sep"
        Case $monthS = 10
            $monthS = "Oct"
        Case $monthS = 11
            $monthS = "Nov"
        Case $monthS = 12
            $monthS = "Dec"
    EndSelect
    $dateS = $monthS & " " & $dayS
    $start = $dateS & " " & $hourS
    Select
        Case $monthE = 1
            $monthE = "Jan"
        Case $monthE = 2
            $monthE = "Feb"
        Case $monthE = 3
            $monthE = "Mar"
        Case $monthE = 4
            $monthE = "Apr"
        Case $monthE = 5
            $monthE = "May"
        Case $monthE = 6
            $monthE = "Jun"
        Case $monthE = 7
            $monthE = "Jul"
        Case $monthE = 8
            $monthE = "Aug"
        Case $monthE = 9
            $monthE = "Sep"
        Case $monthE = 10
            $monthE = "Oct"
        Case $monthE = 11
            $monthE = "Nov"
        Case $monthE = 12
            $monthE = "Dec"
    EndSelect
    $dateE = $monthE & " " & $dayE
    $end = $dateE & " " & $hourE
EndFunc

Func FindStart1()
    $log = FileOpen("C:\Program Files\Everquest\Logs\eqlog_" & $player & "_erollisi.txt", 0)
    While 1
        $line = FileReadLine($log)
        If @error = -1 Then HourInc1()
        If StringInStr($line, $start) <> 0 Then
            OpenFileWhoAll()
        EndIf
    Wend
EndFunc

Func OpenFileWhoAll()
    $loots = FileOpen("C:\Program Files\Everquest\Logs\" & $monthS & "_" & $daystored & ".txt", 2)
    $loot2 = FileOpen("C:\Program Files\Everquest\Logs\TempLoots_" & $monthS & "_" & $daystored & ".txt", 2)
    FileWriteLine($loots,"~~~> STARTING LOG PARSE: " & $monthS &  " " & $dayS & " " & $hourS & ":00")
    While 1
        $line = FileReadLine($log)
        If @error = -1 Then ExitLoop
        If StringInStr($line, $end) <> 0 Then
            ExitLoop
        EndIf
        Select
            Case StringInStr($line, "<Triune>") <> 0
                If StringInStr($line, "[ANONYMOUS]") = 0 Then
                    FileWriteLine($loots, $line)
                EndIf
            Case StringInStr($line, "tells triloot") <> 0
                If StringInStr($line, "grat") <> 0 Then
                    FileWriteLine($loot2, $line)
                EndIf
        EndSelect
    Wend
EndFunc

Func HourInc1()
    $hourS = $hourS + 1
    If $hourS = 24 and $pmS = $GUI_CHECKED Then
        $hourS = "00"
        $dayS = $dayS + 1
            If StringLen($dayS) < 2 Then
                $dayS = "0" & $dayS
            EndIf
    ElseIf $hourS = $hourE Then
        MoveLoots()
        Exit
    EndIf

    $dateS = $monthS & " " & $dayS
    $start = $dateS & " " & $hourS
    FileClose($log)
    FindStart1()
EndFunc

Func MoveLoots()
    MsgBox(1,"","moving loots")
    Local $vPath = $monthS & "_" & $daystored & ".txt"
    Local $tPath = $ePath & "TempLoots_" & $vPath
    $loots = FileOpen($ePath & $vPath, 1)
    $loot2 = _FileReadToArray($tPath,$aLoot2)
    If IsArray($aLoot2) = 1 Then
        _FileWriteFromArray($loots,$loot2)
    Else
        Return
    EndIf
    FileClose($loots)
EndFunc

Edited by Ealric

My Projects: [topic="89413"]GoogleHack Search[/topic], [topic="67095"]Swiss File Knife GUI[/topic], [topic="69072"]Mouse Location Pointer[/topic], [topic="86040"]Standard Deviation Calculator[/topic]

Share this post


Link to post
Share on other sites

Even that does the same thing... creates the files but never copies the lines from one text file to the other. Either I did something wrong in the code, or... I'm just confused :)

Share this post


Link to post
Share on other sites

Just try a debug to make sure it's reading it correctly:

Func MoveLoots()
    MsgBox(1,"","moving loots")
    local $x
    Local $vPath = $monthS & "_" & $daystored & ".txt"
    Local $tPath = $ePath & "TempLoots_" & $vPath
    $loots = FileOpen($ePath & $vPath, 1)
    $loot2 = _FileReadToArray($tPath,$aLoot2)
    If IsArray($aLoot2) = 1 Then
        For $x = 1 to $aLoot2[0]
            MsgBox(0,"Array","$aLoot2[" & $x & "] = " & $aLoot2[$x],1)
        Next
    Else
        Return
    EndIf
    FileClose($loots)
EndFunc

My Projects: [topic="89413"]GoogleHack Search[/topic], [topic="67095"]Swiss File Knife GUI[/topic], [topic="69072"]Mouse Location Pointer[/topic], [topic="86040"]Standard Deviation Calculator[/topic]

Share this post


Link to post
Share on other sites

Just try a debug to make sure it's reading it correctly:

Func MoveLoots()
    MsgBox(1,"","moving loots")
    local $x
    Local $vPath = $monthS & "_" & $daystored & ".txt"
    Local $tPath = $ePath & "TempLoots_" & $vPath
    $loots = FileOpen($ePath & $vPath, 1)
    $loot2 = _FileReadToArray($tPath,$aLoot2)
    If IsArray($aLoot2) = 1 Then
        For $x = 1 to $aLoot2[0]
            MsgBox(0,"Array","$aLoot2[" & $x & "] = " & $aLoot2[$x],1)
        Next
    Else
        Return
    EndIf
    FileClose($loots)
EndFunc
The message box says: "$aLoot2[1]="

I had a message box doing sort of the same thing (trying to display the line it was reading), but it only worked once... and without changing any of the code, never worked again :)

Share this post


Link to post
Share on other sites

The message box says: "$aLoot2[1]="

I had a message box doing sort of the same thing (trying to display the line it was reading), but it only worked once... and without changing any of the code, never worked again :)

Are the files it is reading empty? Or do they contain something? It looks like they are empty. It's not reading 0 kb files is it?

My Projects: [topic="89413"]GoogleHack Search[/topic], [topic="67095"]Swiss File Knife GUI[/topic], [topic="69072"]Mouse Location Pointer[/topic], [topic="86040"]Standard Deviation Calculator[/topic]

Share this post


Link to post
Share on other sites

Are the files it is reading empty? Or do they contain something? It looks like they are empty. It's not reading 0 kb files is it?

Files aren't empty, unless it's trying to open the wrong ones for some reason. I've copied that last function out to it's own .au3 and run it and it does exactly what it's supposed to do...

Share this post


Link to post
Share on other sites

Files aren't empty, unless it's trying to open the wrong ones for some reason. I've copied that last function out to it's own .au3 and run it and it does exactly what it's supposed to do...

That's good news. :)

Okay, I'll start looking over the other functions you created and see if I can figure out what might be going on.


My Projects: [topic="89413"]GoogleHack Search[/topic], [topic="67095"]Swiss File Knife GUI[/topic], [topic="69072"]Mouse Location Pointer[/topic], [topic="86040"]Standard Deviation Calculator[/topic]

Share this post


Link to post
Share on other sites

That's good news. :)

Okay, I'll start looking over the other functions you created and see if I can figure out what might be going on.

Thanks for taking the time... I haven't been able to notice anything, but I'm not perfect :)

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

I notice in OpenFileWhoAll() you aren't closing any of the open files. You should close them.

Func OpenFileWhoAll()
    $loots = FileOpen("C:\Program Files\Everquest\Logs\" & $monthS & "_" & $daystored & ".txt", 2)
    $loot2 = FileOpen("C:\Program Files\Everquest\Logs\TempLoots_" & $monthS & "_" & $daystored & ".txt", 2)
    FileWriteLine($loots,"~~~> STARTING LOG PARSE: " & $monthS &  " " & $dayS & " " & $hourS & ":00")
    While 1
        $line = FileReadLine($log)
        If @error = -1 Then ExitLoop
        If StringInStr($line, $end) <> 0 Then
            ExitLoop
        EndIf
        Select
            Case StringInStr($line, "<Triune>") <> 0
                If StringInStr($line, "[ANONYMOUS]") = 0 Then
                    FileWriteLine($loots, $line)
                EndIf
            Case StringInStr($line, "tells triloot") <> 0
                If StringInStr($line, "grat") <> 0 Then
                    FileWriteLine($loot2, $line)
                EndIf
        EndSelect
    Wend
    FileClose($loots)
    FileClose($loot2)
EndFunc
Edited by Ealric

My Projects: [topic="89413"]GoogleHack Search[/topic], [topic="67095"]Swiss File Knife GUI[/topic], [topic="69072"]Mouse Location Pointer[/topic], [topic="86040"]Standard Deviation Calculator[/topic]

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

I notice in OpenFileWhoAll() you aren't closing any of the open files. You should close them.

Func OpenFileWhoAll()
    $loots = FileOpen("C:\Program Files\Everquest\Logs\" & $monthS & "_" & $daystored & ".txt", 2)
    $loot2 = FileOpen("C:\Program Files\Everquest\Logs\TempLoots_" & $monthS & "_" & $daystored & ".txt", 2)
    FileWriteLine($loots,"~~~> STARTING LOG PARSE: " & $monthS &  " " & $dayS & " " & $hourS & ":00")
    While 1
        $line = FileReadLine($log)
        If @error = -1 Then ExitLoop
        If StringInStr($line, $end) <> 0 Then
            ExitLoop
        EndIf
        Select
            Case StringInStr($line, "<Triune>") <> 0
                If StringInStr($line, "[ANONYMOUS]") = 0 Then
                    FileWriteLine($loots, $line)
                EndIf
            Case StringInStr($line, "tells triloot") <> 0
                If StringInStr($line, "grat") <> 0 Then
                    FileWriteLine($loot2, $line)
                EndIf
        EndSelect
    Wend
    FileClose($loots)
    FileClose($loot2)
EndFunc
I just tried it like that, and for some reason it causes it to not put any information into the second file ($loot2)... Which doesn't make any sense, because it shouldn't leave the Loop until it reaches the end of the $log file and it can't reach the end of $log without going through lines that meet the criteria for the second Case :) Edited by Waeolwen

Share this post


Link to post
Share on other sites

Err... changed the two FileOpen's to "1" (Append mode) and magically everything worked...

*sigh* Of course, the easiest thing does it.

Thanks for the help y'all! :)

Share this post


Link to post
Share on other sites

Err... changed the two FileOpen's to "1" (Append mode) and magically everything worked...

*sigh* Of course, the easiest thing does it.

Thanks for the help y'all! :)

I was just about to ask you if you wanted to overwrite or append on your logs. Glad you found it. In the future, I'd recommend you keep a clean script and a debug script that houses msgboxes for each of the cases, functions, etc. you enter. It helps out a lot when you see what's going on.


My Projects: [topic="89413"]GoogleHack Search[/topic], [topic="67095"]Swiss File Knife GUI[/topic], [topic="69072"]Mouse Location Pointer[/topic], [topic="86040"]Standard Deviation Calculator[/topic]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0