t0ddie Posted May 29, 2005 Posted May 29, 2005 i remember asking a while back, and there was no such or similar function. it would be nice to say... remove line 66 in test.txt or remove the last line or the first is there an easy way yet? or a way at all? or no Valik Note Added 19 October 2006 - 08:38 AMAdded to warn level I just plain don't like you.
GaryFrost Posted May 29, 2005 Posted May 29, 2005 (edited) #include <Array.au3> #include <file.au3> _FileDeleteLine(@MyDocumentsDir & "\output.txt", 5) Func _FileDeleteLine($s_file, $i_line) Dim $aRecords, $fileout, $x If Not _FileReadToArray($s_file, $aRecords) Then MsgBox(4096, "Error", " Error reading " & $s_file & " to Array error:" & @error) Exit EndIf If ($i_line < 1 Or $i_line > $aRecords[0]) Then MsgBox(0, "Error", "Invalid Line # to delete") Exit EndIf _ArrayDelete($aRecords, $i_line) $aRecords[0] = ($aRecords[0]) - 1 $fileout = FileOpen($s_file, 2) ; Check if file opened for writing OK If $fileout = -1 Then MsgBox(0, "Error", "Unable to open " & $s_file) Exit EndIf For $x = 1 To $aRecords[0] - 1 FileWrite($fileout, $aRecords[$x] & @LF) Next FileWrite($fileout, $aRecords[$aRecords[0]]) FileClose($fileout) EndFunc ;==>_FileDeleteLine Edited May 29, 2005 by gafrost SciTE for AutoItDirections for Submitting Standard UDFs Don't argue with an idiot; people watching may not be able to tell the difference.
t0ddie Posted May 29, 2005 Author Posted May 29, 2005 hmmm, i have a folder on my desktop named keychanger and in it i have a .txt file named "keys.txt" in keys.txt i have this one two three four five six i made a new autoit script and ran this and nothing happened... #include Array.au3 #include file.au3 _FileDeleteLine(@DesktopDir & "\keychanger\keys.txt", 5) Func _FileDeleteLine($s_file, $i_line) Dim $aRecords, $fileout, $x If Not _FileReadToArray($s_file, $aRecords) Then MsgBox(4096, Error, Error reading & $s_file & to Array error & @error) Exit EndIf _ArrayDelete($aRecords, $i_line) $aRecords[0] = ($aRecords[0]) - 1 $fileout = FileOpen($s_file, 2) ; Check if file opened for writing OK If $fileout = -1 Then MsgBox(0, Error, Unable to open & $s_file) Exit EndIf For $x = 1 To $aRecords[0] - 1 FileWrite($fileout, $aRecords[$x] & @LF) Next FileWrite($fileout, $aRecords[$aRecords[0]]) FileClose($fileout) EndFunc ;==_FileDeleteLine it should delete "five" am i missing something? Valik Note Added 19 October 2006 - 08:38 AMAdded to warn level I just plain don't like you.
/dev/null Posted May 29, 2005 Posted May 29, 2005 i remember asking a while back, and there was no such or similar function.it would be nice to say... remove line 66 in test.txtor remove the last lineor the firstNo error checking at all! Leave that up to you! Function is NOT very fast on large files!!#include <File.au3> $test = "C:\test.txt" ;msgbox(4096, "Info", "Deleting line Nr. 1") ;FileDelLine($test, 1) ; Delete 1 th line ;msgbox(4096, "Info", "Deleting line Nr. 4") ;FileDelLine($test, 4) ; Delete 4 th line msgbox(4096, "Info", "Deleting last line") FileDelLine($test, _FileCountLines($test)) ; Delete last line func FileDelLine($file, $del_line) local $tmpfile = _TempFile() local $fh = FileOpen ( $file, 0) local $fh_tmp = FileOpen ( $tmpfile, 2 ) local $cur_line = 0 while 1 $line = FileReadLine($fh) $cur_line = $cur_line + 1 if @error = -1 then FileClose($fh) FileClose($fh_tmp) FileMove($file, $file & ".old") FileMove($tmpfile, $file) return endif if $cur_line <> $del_line then FileWriteLine($fh_tmp, $line) endif wend endfuncCheersKurt __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *
GaryFrost Posted May 29, 2005 Posted May 29, 2005 Is it on just your dekstop or all user desktop? Modified above script to make sure line is valid. SciTE for AutoItDirections for Submitting Standard UDFs Don't argue with an idiot; people watching may not be able to tell the difference.
t0ddie Posted May 29, 2005 Author Posted May 29, 2005 ok.... i try this. and it works once.... it deletes the last line.. but i try this.. and it only deletes one line! expandcollapse popup#include <File.au3> $test = @DesktopDir & "\keychanger\keys.txt" ;msgbox(4096, "Info", "Deleting line Nr. 1") ;FileDelLine($test, 1); Delete 1 th line ;msgbox(4096, "Info", "Deleting line Nr. 4") ;FileDelLine($test, 4); Delete 4 th line msgbox(4096, "Info", "Deleting last line") FileDelLine($test, _FileCountLines($test)); Delete last line msgbox(4096, "Info", "Deleting last line") FileDelLine($test, _FileCountLines($test)); Delete last line msgbox(4096, "Info", "Deleting last line") FileDelLine($test, _FileCountLines($test)); Delete last line msgbox(4096, "Info", "Deleting last line") FileDelLine($test, _FileCountLines($test)); Delete last line func FileDelLine($file, $del_line) local $tmpfile = _TempFile() local $fh = FileOpen ( $file, 0) local $fh_tmp = FileOpen ( $tmpfile, 2 ) local $cur_line = 0 while 1 $line = FileReadLine($fh) $cur_line = $cur_line + 1 if @error = -1 then FileClose($fh) FileClose($fh_tmp) FileMove($file, $file & ".old") FileMove($tmpfile, $file) return endif if $cur_line <> $del_line then FileWriteLine($fh_tmp, $line) endif wend endfunc it should delete the four last lines, but it doesnt.. i just pretty much need to delete the 4 last lines in the text file, still trying to get there.. heh. Valik Note Added 19 October 2006 - 08:38 AMAdded to warn level I just plain don't like you.
/dev/null Posted May 29, 2005 Posted May 29, 2005 (edited) ok....i try this. and it works once....it deletes the last line..but i try this.. and it only deletes one line!Well, as I said: Absolutely no error checking, leave that up to you.Anyway, FileMove fails, if the .old file already exists. I modified the code to deletethe input file, before FileMove temp file. However, code can still fail! You will have to check the return codes after each File function call if you want a reliable solution!!!while 1 $line = FileReadLine($fh) $cur_line = $cur_line + 1 if @error = -1 then FileClose($fh) FileClose($fh_tmp) ; FileMove($file, $file & ".old"); <== changed HERE! FileDelete($file);; <== changed HERE! FileMove($tmpfile, $file) return endifCheersKurt Edited May 29, 2005 by /dev/null __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *
/dev/null Posted May 29, 2005 Posted May 29, 2005 (edited) Here is an updated version of the script, as the last version had to go through the whole file four times. Now, you can specify to delete from line x to y. Still no error checking! e.g. I don't check if x < y. #include <File.au3> $test = "C:\test.txt" msgbox(4096, "Info", "Deleting Last 4 lines ") $lastline = _FileCountLines($test) FileDelLine($test, $lastline-3, $lastline) msgbox(4096, "Info", "Deleting line 2 ") FileDelLine($test, 2, 2) func FileDelLine($file, $start_line, $end_line) local $tmpfile = _TempFile() local $fh = FileOpen ( $file, 0) local $fh_tmp = FileOpen ( $tmpfile, 2 ) local $cur_line = 0 while 1 $line = FileReadLine($fh) $cur_line = $cur_line + 1 if @error = -1 then FileClose($fh) FileClose($fh_tmp) FileDelete($file) FileMove($tmpfile, $file) return endif if ($cur_line < $start_line) or ($cur_line > $end_line) then FileWriteLine($fh_tmp, $line) endif wend endfunc Cheers Kurt Edited May 29, 2005 by /dev/null __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *
Marius Posted June 28, 2005 Posted June 28, 2005 (edited) HEY! Great code. But maybe someone could edit it, so blank (deleted) lines are left in file? Edited June 28, 2005 by Marius Marius back in da hood! :)
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