Jump to content

My loop to check for file size increase is not working.


Avro
 Share

Recommended Posts

I know I'm a noob. I've only been playing with autoit for a week now. I haven't tried to write anything for almost 30 years.

I've troubleshooted everything except this problem. I just can't see the error in my flow.

Background:

I'm writing a game script.. ( I know, boo to me ).

The problem I'm having has nothing to do with gaming.

I'm trying to parse the chat log file of the game. It is a standard text file with @CRLF at the end of each line.

I've read the file into an array or one line at a time. I've tried to check the file size for growth but no luck. I tried a loop with FileReadLine and _Filecountlines but I ran into the same problem as FileGetSize. It won't update when a different program is adding the lines. I believe it has to do with info remaining in the file buffers.

I can create the log file for the game or let the game create it. I can open an existing chatlog and read it line by line or into an array and read it and parse it fine. If the games adds to the log, I can't see it that change.

What I can't do is get new lines that the game adds to the log file. My code won't notice file growth.

I can add a dozen lines to the chatlog text file while the script is in its main loop and it just sits there at the same length until I quit the script and rerun it.

#Include <Misc.au3>
#Include <File.au3>
#Include <String.au3>
#include <Array.au3>

;Initialize Global Variable
Global Const $Chatlog = "chatlog.txt"
Global $CountLines, $Chatlength, $file, $line, $Secret, $Chatline

HotKeySet("{ESC}", "Terminate")
HotKeySet("{PAUSE}", "TogglePause")

;move cursor to and activate SWG
Sleep (1000)
WinActivate( "Star Wars Galaxies", ""); my game that adds to the "chatlog.txt"
Sleep (100)

$Secret = 1
$Paused = 0
$CountLines = _FileCountLines($Chatlog)
$Chatlength = $CountLines
$chatsize = FileGetSize($Chatlog)
ToolTip($CountLines & " Lines found", 50, 50, "",0 ,1) ;text file has been 5 to 150 lines long
Sleep(5000)
ToolTip("",50,50)

While $Secret ; is true till I put in new code
    $chksize = FileGetSize($Chatlog) ; most current size of chatlog
    if $chatsize < $chksize Then ;if current size is larger then continue
        If Not _FileReadToArray($Chatlog,$Chatline) Then ;Read file into $Chatline[] array
            MsgBox(4096,"Error", " Error reading Chat log to Array     error:" & @error)
            Exit
        EndIf
        If $Chatlength <> $CountLines Then
        $CountLines = $Chatline[0] ;get number of lines in file
        For  $i = $Chatlength to $CountLines ;loop from last chatlog line number to current last line
            MsgBox(0,String($i) & "New line", $chatline[$i]) ;works fine till $CountLines = $Chatlength the first time
        Next ;My Parser goes in this loop when it works :-)
        $Chatlength = $CountLines
        EndIf
    $chatsize = $chksize
    EndIf
WEnd

Exit

Func Terminate()
    Exit 0
EndFunc

Func TogglePause()
    $Paused = NOT $Paused
    While $Paused
        sleep(100)
        ToolTip('Script is "Paused"',0,0)
    WEnd
    ToolTip("")
EndFunc

I really like the potential of this program.

Link to comment
Share on other sites

Avro hopefully this will help you...

;************************************************
;Checks txt file line count every two seconds    
;and returns a msgbox, or whatever function when 
;the file count changes in realtime.             
;                                                
;               LOVE AUTOIT                          
;               Aug 27 09'                       
;************************************************

#Region Author: Overflo.

#Include <File.au3>

;set the file to read or <chatlog>?
Global $file = "c:\music.txt" 

; count original lines at time of open
$oldlines = _FileCountLines($file) 

; close the file just because im not sure if _FileCountLines does it
FileClose($file) 

; you can remove this, just to see the original file count.
MsgBox(0,'Original Lines',$oldlines) 

; make it run like jackie joyner kersee
while 1
    
    $newlines = _FileCountLines($file) ;count the txt filelines...
    IF $newlines > $oldlines Then ;compares new with old
        
        msgbox(0,'','file has changed, it now has ' & $newlines & ' line(s)') ;;send feedback through msgbox in this case, but throw any functions you want in!
        $oldlines = $oldlines + 1 ;add +1 after original linecount to keep it real time.
    EndIf
    
    sleep(2000) ; sleeping it, save your CPU! ; checks every 2 seconds, change to your liking.
WEnd

if you create a music.txt file in your c:\ add a couple lines, save it. then run this script... it will tell you how many lines you have currently and then wait for changes. now you can go back in music.txt while the script is running and add a line or 20 then save it, the script should respond within 2 seconds.

Edited by overflo

;Frank. 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...