Jump to content

Read the last line of a .txt file


ExElNeT
 Share

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Moderators

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

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