Vikramjeet Posted January 28, 2021 Posted January 28, 2021 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
Nine Posted January 28, 2021 Posted January 28, 2021 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. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
Vikramjeet Posted January 29, 2021 Author Posted January 29, 2021 ;***************************************************************** 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.
Vikramjeet Posted January 29, 2021 Author Posted January 29, 2021 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')
FrancescoDiMuro Posted January 29, 2021 Posted January 29, 2021 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 Werty and Musashi 1 1 Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette
Nine Posted January 29, 2021 Posted January 29, 2021 (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 January 29, 2021 by Nine FrancescoDiMuro 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
junkew Posted January 29, 2021 Posted January 29, 2021 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? FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now