Shade Posted February 1, 2007 Posted February 1, 2007 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. [u]Window Hider[/u]
smstroble Posted February 1, 2007 Posted February 1, 2007 (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 February 1, 2007 by smstroble MUHAHAHAHAHA
Shevilie Posted February 1, 2007 Posted February 1, 2007 $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
Shade Posted February 1, 2007 Author Posted February 1, 2007 Thanks for the quick replies! I finally solved my problem. [u]Window Hider[/u]
Shevilie Posted February 1, 2007 Posted February 1, 2007 (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 February 1, 2007 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now