Excalibur Posted May 2, 2006 Share Posted May 2, 2006 What im trying to accomplish is removing the last line in a file completely. Best way I've come up with using whats available in AutoIt is to copy the entire contents of the file, and write it back without the last line. But I'm sure there is a better way... If I could set which line of the file the WriteLine command would write to, that would be sufficent aswell. Ooo Ëxçã¿îbúr ooO"Information Is Not Knowledge." ~Albert Einstein Link to comment Share on other sites More sharing options...
Thatsgreat2345 Posted May 2, 2006 Share Posted May 2, 2006 check out Filecountlines Link to comment Share on other sites More sharing options...
greenmachine Posted May 2, 2006 Share Posted May 2, 2006 _FileWriteToLine() in the beta (somewhere around .100?). Latest will definitely have it. Link to comment Share on other sites More sharing options...
Excalibur Posted May 2, 2006 Author Share Posted May 2, 2006 greenmachine: by executing that statement once, will any future WriteLine commands continue where _FileWriteToLine() left off? Or should I just test it out and see. Ooo Ëxçã¿îbúr ooO"Information Is Not Knowledge." ~Albert Einstein Link to comment Share on other sites More sharing options...
Thatsgreat2345 Posted May 2, 2006 Share Posted May 2, 2006 (edited) no , you specify the line to right to so yeah.... Edited May 2, 2006 by thatsgreat2345 Link to comment Share on other sites More sharing options...
Excalibur Posted May 2, 2006 Author Share Posted May 2, 2006 *sighs* oh well, screw it, im doing it my way then... the SLOW way... I'll check this again tommorrow and see if anything good has turned up. Thx for the help. Ooo Ëxçã¿îbúr ooO"Information Is Not Knowledge." ~Albert Einstein Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted May 2, 2006 Moderators Share Posted May 2, 2006 (edited) *sighs* oh well, screw it, im doing it my way then... the SLOW way... I'll check this again tommorrow and see if anything good has turned up. Thx for the help.Don't give up so easy lol... You need beta for this but:#include <File.Au3> $FileLocation = @DesktopDir & '\Au3Test.txt' $TextToWrite = '' _FileWriteToLine($FileLocation, _FileCountLines($FileLocation), $TextToWrite, 1)Edit: Had way too much code for such an easy function, although this does not in fact delete the last line ... only wipes it out Edited May 2, 2006 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 More sharing options...
Excalibur Posted May 2, 2006 Author Share Posted May 2, 2006 Thanks smoke, but I've concidered your solution, what it would do is leave a return on that line causing skips in the text file. Ooo Ëxçã¿îbúr ooO"Information Is Not Knowledge." ~Albert Einstein Link to comment Share on other sites More sharing options...
greenmachine Posted May 2, 2006 Share Posted May 2, 2006 Don't give up so easy lol... You need beta for this but:#include <File.Au3> $FileLocation = @DesktopDir & '\Au3Test.txt' $TextToWrite = '' _FileWriteToLine($FileLocation, _FileCountLines($FileLocation), $TextToWrite, 1)Edit: Had way too much code for such an easy function, although this does not in fact delete the last line ... only wipes it out To fully delete the line, couldn't you just change the previous line to not have an endline character at the end? It takes two _FileWriteToLine functions - the first to clear the last line, and the second to erase the endline character in the second-to-last line. Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted May 2, 2006 Moderators Share Posted May 2, 2006 (edited) To fully delete the line, couldn't you just change the previous line to not have an endline character at the end? It takes two _FileWriteToLine functions - the first to clear the last line, and the second to erase the endline character in the second-to-last line.I decided to just write a UDF for it, it works as far as I can tell:$FileLocation = @DesktopDir & '\Au3Test.txt' _FileDeleteLine($FileLocation) Func _FileDeleteLine($hFile, $i_Last = 0); If other than 0 it will delete whatever line you want Local $hFileOpen = '', $aArrayRead = StringSplit(FileRead($hFile, FileGetSize($hFile)), @CRLF, 1) $hFileOpen = FileOpen($hFile, 2) If $i_Last = 0 And UBound($aArrayRead) - 1 > 0 Then For $i_Count = 1 To UBound($aArrayRead) - 2 If $i_Count = UBound($aArrayRead) - 2 Then FileWrite($hFileOpen, $aArrayRead[$i_Count]) Else FileWrite($hFileOpen, $aArrayRead[$i_Count] & @CRLF) EndIf Next FileClose($hFileOpen) Return 1 ElseIf $i_Last <> 0 And UBound($aArrayRead) - 1 > 0 Then For $i_Count = 1 To UBound($aArrayRead) - 1 If $i_Count = $i_Last Then ContinueLoop If $i_Count = UBound($aArrayRead) - 1 Then FileWrite($hFileOpen, $aArrayRead[$i_Count]) Else FileWrite($hFileOpen, $aArrayRead[$i_Count] & @CRLF) EndIf Next FileClose($hFileOpen) Return 1 EndIf FileClose($hFileOpen) Return 0 EndFuncYou can leave the last parameter empty and it will delete the last line, or you can put a number in and it will delete that line specifically. Edit: I just noticed I had a variable in there that wasn't being used. Edited May 2, 2006 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 More sharing options...
Moderators SmOke_N Posted May 5, 2006 Moderators Share Posted May 5, 2006 @Excalibur - Did this work for you? 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 More sharing options...
exodius Posted May 5, 2006 Share Posted May 5, 2006 It worked for me when I tested it. Good job smoke. Link to comment Share on other sites More sharing options...
Valuater Posted May 5, 2006 Share Posted May 5, 2006 using array pop #include <file.au3> #include <Array.au3> Dim $aRecords If Not _FileReadToArray("test.txt",$aRecords) Then MsgBox(4096,"Error", " Error reading log to Array error:" & @error) Exit EndIf MsgBox(0,'Line removed',_ArrayPop($aRecords)) _ArrayDisplay( $aRecords, "Entries left in the array" ) 8) Link to comment Share on other sites More sharing options...
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