Sign in to follow this  
Followers 0
LarsJ

Read the last lines added to a logfile

5 posts in this topic

Are there better ways to do it than this:

#include <Array.au3>
$file = FileOpen( "file.log", 16 )                                          ; Open the file in binary mode
FileSetPos( $file, $lastFileSize+1, 0 )                                     ; Move filepointer forward
$bytes = BinaryToString( FileRead( $file, $thisFileSize - $lastFileSize ) ) ; Read the new bytes
FileClose( $file )
$aLines = StringSplit( $bytes, @CRLF, 3 )                                   ; Split the bytes into an array of lines
_ArrayDisplay( $aLines )

Although it's simple code it's pretty fast. But I would like to know if there are more advanced methods to make it even faster.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Here another method:

$hFile = FileOpen(@ScriptDir & "file.log")
$string = FileReadLine($hFile, -1)
FileClose($hFile)
MsgBox(0, "Test", $string)

I didn't benchmark it.

_FileReadToArray() might be also a possibility whereas last element in array is the last line.

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

UEZ,

The logfile can grow up till 2 GB. I record the filesize, so I know how many bytes is added, but I don't know how many lines. So FileReadLine and _FileReadToArray will not work in this case. After I read your post I tested how fast the method in post #1 is. Reading and splitting the last 100 KB (about 1000 lines) in a 1.2 GB logfile is done in 40 ms. Pretty fast with just build in commands. This can probably not be done any better.

Lars.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I had a similar problem to read the result of the huge robocopy log file whereas I need only a few lines from the tail.

Have a look to

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Thank you. I have just downloaded your script. I had a short look at it some days ago. It matched a search for "FileSetPos".

Lars.

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