PcExpert Posted January 1, 2008 Posted January 1, 2008 Hi all, Is there a function in autoit to delete a specified line? Thanks!
Paulie Posted January 1, 2008 Posted January 1, 2008 not a built-in function that i know of, but i suppose you could _FileReadToArray() and then _ArrayDelete() the line you want to remove.
NeuroToxic Posted January 1, 2008 Posted January 1, 2008 Func _Delete_Line_In_File($fileName, $lineNum) Local $len = FileGetSize($fileName) Local $txt = FileRead($fileName, $len) If @error = 1 Then Return 0 Local $pos1 = StringInStr($txt, @lf, 0, $lineNum - 1) Local $pos2 = StringInStr($txt, @lf, 0, $lineNum) If $pos1 > 0 Or $pos2 > 0 Then If $pos2 = 0 Then $pos2 = $len FileDelete($fileName) FileWrite($fileName, StringMid($txt, 1, $pos1) & StringMid($txt, $pos2 + 1)) Return 1 EndIf Return 0 EndFunc ;==> _Delete_Line_In_File() Deathdn and faldo 2
rasim Posted January 2, 2008 Posted January 2, 2008 Hi all, Is there a function in autoit to delete a specified line? Thanks! More solutions #include <Array.au3> #include <File.au3> Global $StrArray[1] DeleteString(@ScriptDir & "\test.txt", 3) Func DeleteString($sFileName, $sLine) Local $FileHwnd If Not FileExists($sFileName) Then MsgBox(16, "Error", "File not exist") Exit EndIf _FileReadToArray($sFileName, $StrArray) _ArrayDelete($StrArray, $sLine) ;_ArrayDisplay($StrArray) $FileHwnd = FileOpen($sFileName, 2) For $i = 1 To UBound($StrArray) -1 FileWriteLine($FileHwnd, $StrArray[$i]) Next FileClose($FileHwnd) EndFunc
goldenix Posted January 2, 2008 Posted January 2, 2008 Small note, I dont recommend using arrays. Array is slow as hell. Especially if you have files with around 100 000 lines in them or you simply phrise many files. String thing is alot faster. My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
PcExpert Posted January 2, 2008 Author Posted January 2, 2008 So there isnt a function like this: FileDeleteLine("mytextfile.txt", "TThis line is ready to be deleted.")
BigDod Posted January 2, 2008 Posted January 2, 2008 _FileWriteToLine RemarksIf _FileWriteToLine is called with $fOverWrite as 1 and $sText as "", it will delete the line. Time you enjoyed wasting is not wasted time ......T.S. Elliot Suspense is worse than disappointment................Robert Burns God help the man who won't help himself, because no-one else will...........My Grandmother
PcExpert Posted January 3, 2008 Author Posted January 3, 2008 @Big Dod How to use that function if I dont know wich line I want to delete, I know the text, but not the line number.
jackbanditdude Posted February 21, 2012 Posted February 21, 2012 This is an old post, but if you attempted to use _FileWriteToLine() recently and for any reason it could not open, NeuroToxic's solution works like a charm. Simply put it into a for loop using FileReadLine to find your line number with the matching string and then use the _Delete_Line_In_File() function. _FileWriteToLine() could not open my file, regardless how I gave it the file, closing it, opening it, etc. _ReplaceStringInFile() wasn't a very good choice either for what I was doing. I'll hold on to NeuroToxic's little code snippet for sure. Adding
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