Jump to content

Read the last lines added to a logfile


LarsJ
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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

Link to comment
Share on other sites

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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

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...