Jump to content
Sign in to follow this  
ExElNeT

Read the last line of a .txt file

Recommended Posts

ExElNeT

I am trying to read the last line of a .txt file everytime a new line gets inserted by another program in the .txt file.

I ve tried this:

#include <File.au3>

$file = FileOpen("test.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

        $lineCount = _FileCountLines("test.txt")
        $line = FileReadLine($file, $lineCount)
        If @error = -1 Then ExitLoop; end of file
        If @error = 1 Then
            MsgBox(0,"error","Cannot read line")
        EndIf       
        MsgBox(0,"LineCount", $lineCount & ' ' & $line)
FileClose($file)

But the output is wrong. The output line is not the last line in the .txt file

thanks in advance

Share this post


Link to post
Share on other sites
Ted at Miles

I am trying to read the last line of a .txt file everytime a new line gets inserted by another program in the .txt file.

I ve tried this:

#include <File.au3>

$file = FileOpen("test.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

        $lineCount = _FileCountLines("test.txt")
        $line = FileReadLine($file, $lineCount)
        If @error = -1 Then ExitLoop; end of file
        If @error = 1 Then
            MsgBox(0,"error","Cannot read line")
        EndIf       
        MsgBox(0,"LineCount", $lineCount & ' ' & $line)
FileClose($file)

But the output is wrong. The output line is not the last line in the .txt file

thanks in advance

Share this post


Link to post
Share on other sites
Ted at Miles

I have messed with the line count command. Does not work most of the time. It is supposed to work for server logs from what I have heard.

good luck

Share this post


Link to post
Share on other sites
ExElNeT

I have messed with the line count command. Does not work most of the time. It is supposed to work for server logs from what I have heard.

good luck

the line number is correct. but the output doesnt match that.

Share this post


Link to post
Share on other sites
SmOke_N

$aSplit = StringSplit(StringStripCR(FileRead('FileLocation.Name')), @LF)
MsgBox(64, 'Last Line', $aSplit[$aSplit[0]])

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
ExElNeT

thanks i ll try it :=)

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  

×