Jump to content

Reading only the last line


jimmyjoe
 Share

Recommended Posts

How do I use filereadline to read the last line of a text log whenever the size or number of lines of the file is constantly changing. Im trying to read the last line of a chat log after some other actions looking for a certain line of text and when it sees that line of text it will call another function. I just need it to repeat looking at the last line of file as lines are wrote to it until it sees the phase im looking for. Im sure its simple but all im seeing is for the action to read from line 1 to end which i dont believe would be good when the file gets up to 2 mbs or larger. Any help would be appreciated.

Edited by jimmyjoe
Link to comment
Share on other sites

Here's a UDF:

#include <File.au3>
Msgbox(0,"", _FileReadLastLine("Path to file"))

Func _FileReadLastLine($Filepath)
    $File = FileOpen($Filepath, 0)
    $Line = _FileCountLines($FilePath)
    $Result = FileReadLine($File, $Line)
    FileClose($File)
Return $Result
EndFunc
Edited by Paulie
Link to comment
Share on other sites

Just going off the top of my head here but I think there is a _FileGetLineCount() function in the UDFs. You might be able to put that function in a loop and when the line count changes use FileReadLine() to read the last line.

Someone else will probably come up with a simpler method.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Make sure to read the code in _FileGetLineCount(). If I recall right it ain't efficient. Especially if you read it over and over again. My files here are old, so you better check for your selfe..:)

Wonder if there is a topic named tail in here somewhere?

Link to comment
Share on other sites

Here's a UDF:

#include <File.au3>
Msgbox(0,"", _FileReadLastLine("Path to file"))

Func _FileReadLastLine($Filepath)
    $File = FileOpen($Filepath, 0)
    $Line = _FileCountLines($FilePath)
    $Result = FileReadLine($File, $Line)
    Return $Result
EndFunc
Since the function will be called often you will soon run into problems; you need to add FileClose($File) before the function returns.
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Here a simple function that reads a file into an array:

Func _FileToArray($file)
    Local $handle = FileOpen($file, 0)
    If $handle = -1 Then Return SetError(1)
    Local $read[1], $i = 0
    $read[0] = 0
    While 1
        Local $line = FileReadLine($handle)
        If @error = -1 Then ExitLoop
        $i += 1
        $read[0] = $i
        ReDim $read[$i + 1]
        $read[$i] = $line
    WEnd
    FileClose($file)
    Return $read
EndFunc   ;==>_FileToArray

$read[0] contains the numbers of lines, therefore the content of the last line is $read[$read[0]]

Link to comment
Share on other sites

Thanks Paulie.

_FileCountLines() is the one I was thinking of.

Keep it up and you will soon be getting mentioned as my nemesis again. :)

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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