Jump to content

Recommended Posts

Posted

I have a script that reads the last 7 lines of an active log file. By active I mean that the file is being written while the script runs. 

How can I make the script to

1- keep reading the last 7 lines till a specific text appears.

2- Once the specific text appears, then get out of the loop and proceed to other tasks

Thank You

Posted

It depends how fast you need to react when those 7 lines appear ?

How big is this log file ?

what is the string you are looking for ?

And more importantly, what have you tried that did not work.

And  please provide all information necessary to run the script, not just a few lines of random codes.

Posted
;*****************************************************************              
                Local $file = "CAPTURE.log"
                FileOpen($file, 0)
                
                For $t = _FileCountLines($file) - 7 to _FileCountLines($file)
                    $line = FileReadLine($file, $t)
                    $WaitTill = StringInStr($line, "PROCESS COMPLETE")
                    
                        ;Sleep (2000)
                Next
                Do  
                    sleep (1000)
                Until  $WaitTill = "PROCESS COMPLETE"
                        
                
                
;*****************************************************************

I am sending formats using key strokes into an app. The app accepts the format and returns a response. The app takes more time to return a response for certain formats. I want the script to sleep till it gets the defined response (PROCESS COMPLETE). 

I am not calling to read the logs constantly. I only call to read the logs at specific moments during the run and only look at the last 7 lines of the log file. The log file is in a notepad like format and may be up to 5000 lines. After I call the script to look at the last 7 lines of the log file, I want it to wait till it finds 'PROCESS COMPLETE' message in the log file.

Posted
5 hours ago, Nine said:

It depends how fast you need to react when those 7 lines appear ?

How big is this log file ?

what is the string you are looking for ?

And more importantly, what have you tried that did not work.

And  please provide all information necessary to run the script, not just a few lines of random codes.

I figured it. Thank you for looking

Do
    Local $file = "CAPTURE.log"
    FileOpen($file, 0)
        For $t = _FileCountLines($file) - 7 to _FileCountLines($file)
            $line = FileReadLine($file, $t)                     
                If StringInStr($line, 'PROCESS COMPLETE') Then
                    ExitLoop
                EndIf
        Next
Until StringInStr($line, 'PROCESS COMPLETE')

 

Posted
3 hours ago, Vikramjeet said:

FileOpen($file, 0)

This instruction does nothing since you are not storing the handle of the file to use later in _FileCountLines().
Then, you could use AdLibRegister() to repeat the task every specific amount of time, or even use FindFirstChangedNotification() API to see if the file has been modified, and trigger the script to read the last 7 lines of the log file :)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Posted (edited)

@FrancescoDiMuro is right.  Moreover, if you run your script in a loop, you may face a memory leak as the file is never closed.  Either use the file handle and close the handle after usage or remove the FileOpen statement completely.

Edited by Nine
Posted

When your logfiles are big probably using  https://www.autoitscript.com/autoit3/docs/functions/FileSetPos.htm and read backward (either per character or per block of bytes) is more efficient compared to reading the whole file into memory at once.

As you say the logfile is actively written to is it inactive when the PROCESS COMPLETE text is there or could it get loglines written so quick that you miss it as more then 7 newlines where written?

 

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
×
×
  • Create New...