Sign in to follow this  
Followers 0
Shade

Help with reading a file

5 posts in this topic

I was wondering if anyone knew how to read the last line in a file and the second to last, in case the last one is a CR. Your help is appreciated.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

try this

#Include <file.au3>
$path = "C:\whatever.txt"
$file = 0
_FileReadToArray($path , $file) ;loads all lines into $file with $file[0] = number of lines
$lastline = $file[$file[0]-1] ;$file[number of line -1]
$secondtolastline = $file[$file[0]-2] ;$file[number of line -2]
MsgBox(0, "", $lastline $ @CRLF & $secondtolastline)
Edited by smstroble

MUHAHAHAHAHA

Share this post


Link to post
Share on other sites

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

$2ndlast = ""
$last = ""

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

; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $last = $line
    $2ndlast = $last
Wend

FileClose($file)

MsgBox(0,0,"Last line: " & $last & "; 2nd Last: " & $2ndlast)


Start here if you are new Valuater's AutoIT 1-2-3Looking for an UDF - Look hereDo you need to do it twice - Autoit

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Actually found a bug in my script :)

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

$2ndlast = ""
$last = ""

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

; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    ;Need to switch the below as they are now
    $2ndlast = $last
    $last = $line
Wend

FileClose($file)

MsgBox(0,0,"Last line: " & $last & "; 2nd Last: " & $2ndlast)
Edited by Shevilie

Start here if you are new Valuater's AutoIT 1-2-3Looking for an UDF - Look hereDo you need to do it twice - Autoit

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