mcgill Posted October 17, 2006 Share Posted October 17, 2006 Can someone help me with deleting a line from a textfile. Here is my script so far: Func AddtoDelete() $file = "" $find = guictrlread($tab0BranchEditLabel) & "|" & guictrlread($tab0SeriesEditLabel) & "|" _ & guictrlread($tab0BranchIDEditLabel) If GUICtrlRead($BranchColorLabel) = "Blue Payrolls" then $file = FileOpen($pathBlue, 1) ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; I know I need something in here to make it search through the array when import then ;;; delete the line and re write it all back to the array. FileClose($file) Else msgbox(0,"","Error") EndIf EndFunc Link to comment Share on other sites More sharing options...
Xenobiologist Posted October 17, 2006 Share Posted October 17, 2006 HI, have a look at _FileReadToArray() and StringInStr() <> 0 Then ... So long, Mega Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times Link to comment Share on other sites More sharing options...
mcgill Posted October 17, 2006 Author Share Posted October 17, 2006 Here is a bit of code I am playing with before updating my main code to delete a line in a text file. The _FileReadToArray works great. How do I tell it to search for a string instead of pointing it to a variable in the array like 1 - whats in the code now. #include<Array.au3> #include<File.au3> Dim $a_Test $find = "test" ; Read file into array _FileReadToArray("c:\test.txt",$a_Test) ; reverse records _ArrayDelete($a_Test,1) <-- How do I tell it to only delete the line which equals $find ; write reversed array to file _FileWriteFromArray("c:\test2.txt",$a_Test,1) Link to comment Share on other sites More sharing options...
Xenobiologist Posted October 17, 2006 Share Posted October 17, 2006 Hi, one way: #include<Array.au3> #include<File.au3> Dim $Lines_A $find = "search" If Not _FileReadToArray("3.txt", $Lines_A) Then MsgBox(4096, "Error", " Error reading log to Array error:" & @error) Exit (0) EndIf Dim $Second_A[1] For $i = 1 To UBound($Lines_A) - 1 If $Lines_A[$i] <> $find Then _ArrayAdd($Second_A, $Lines_A[$i]) Next _ArrayDisplay($Second_A, "2") _FileWriteFromArray("Ohne3.txt", $Second_A, 1) So long, Mega Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times Link to comment Share on other sites More sharing options...
mcgill Posted October 17, 2006 Author Share Posted October 17, 2006 I tried the following code and everything works until it has to write back into the file. It seems that the line does not write to the file. On the arraydisplay the array is correct by taking out the $find variable. #include<Array.au3> #include<File.au3> Dim $Lines_A $find = "ease 6.5.1" If Not _FileReadToArray("c:\test.txt", $Lines_A) Then MsgBox(4096, "Error", " Error reading log to Array error:" & @error) Exit (0) EndIf Dim $Second_A[1] For $i = 1 To UBound($Lines_A) - 1 If $Lines_A[$i] <> $find Then _ArrayAdd($Second_A, $Lines_A[$i]) Next _ArrayDisplay($Second_A, "2") _FileWriteFromArray("c:\test2.txt", $Second_A, 1) Link to comment Share on other sites More sharing options...
Xenobiologist Posted October 17, 2006 Share Posted October 17, 2006 Hi, what does that mean? the _ArrayDisplay() is correct, but the _FileWriteFromArray at the end doesn't work? So long, Mega Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted October 17, 2006 Moderators Share Posted October 17, 2006 I was fiddling around with different ways to delte a line, if you know the beginning part of the line (at least the first word) this seemed to work well without having to put it into an array. Keep in mind, there is much more you can do with StringRegExp() or Reading the file to an array than you can with this option.$string = 'This is an example' & @CRLF & 'Of deleting a line' & @CRLF & 'If you know at least the beginning text of the line.' MsgBox(0, 'Original', $string) $deleteline = _StringRemoveLine($string, 'Of deleting') MsgBox(0, 'Deleted Line', $deleteline) Func _StringRemoveLine($hFile, $sDelete) If FileExists($hFile) Then $hFile = FileRead($hFile);Remove If FileExists($hFile) Then << only Local $nSNS = StringInStr($hFile, @CRLF & $sDelete) - 1 Local $sSFH = StringLeft($hFile, $nSNS) Local $sRL = StringTrimLeft($hFile, StringLen($sSFH) + 2) Local $sSLH = StringTrimLeft($hFile, StringLen(StringLeft($sRL, _ StringInStr($sRL, @CRLF))) + $nSNS + 2) Return $sSFH & $sSLH EndFunc 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...
Valuater Posted October 17, 2006 Share Posted October 17, 2006 I was fiddling around with different ways to delte a line, if you know the beginning part of the line (at least the first word) this seemed to work well without having to put it into an array. Keep in mind, there is much more you can do with StringRegExp() or Reading the file to an array than you can with this option.$string = 'This is an example' & @CRLF & 'Of deleting a line' & @CRLF & 'If you know at least the beginning text of the line.' MsgBox(0, 'Original', $string) $deleteline = _StringRemoveLine($string, 'Of deleting') MsgBox(0, 'Deleted Line', $deleteline) Func _StringRemoveLine($hFile, $sDelete) If FileExists($hFile) Then $hFile = FileRead($hFile);Remove If FileExists($hFile) Then << only Local $nSNS = StringInStr($hFile, @CRLF & $sDelete) - 1 Local $sSFH = StringLeft($hFile, $nSNS) Local $sRL = StringTrimLeft($hFile, StringLen($sSFH) + 2) Local $sSLH = StringTrimLeft($hFile, StringLen(StringLeft($sRL, _ StringInStr($sRL, @CRLF))) + $nSNS + 2) Return $sSFH & $sSLH EndFunc nifty... SmOke!! 8) Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted October 17, 2006 Moderators Share Posted October 17, 2006 (edited) nifty... SmOke!! 8)I suppose, ... except it doesn't delete the last line if the that's where the text is ... Again... the FileReadToArray() is probably your best bet. Edit: That issue should be fixed with:Func _StringRemoveLine($hFile, $sDelete) If FileExists($hFile) Then $hFile = FileRead($hFile);Remove If FileExists($hFile) Then << only Local $nSNS = StringInStr($hFile, @CRLF & $sDelete) - 1 Local $sSFH = StringLeft($hFile, $nSNS) Local $sRL = StringTrimLeft($hFile, StringLen($sSFH) + 2) Local $sLLEN = StringLen(StringLeft($sRL, StringInStr($sRL, @CRLF))) If Not $sLLEN Then $sLLEN = StringLen($sRL) Return $sSFH & StringTrimLeft($hFile, $sLLEN + $nSNS + 2) EndFunc Edited October 17, 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...
Xenobiologist Posted October 17, 2006 Share Posted October 17, 2006 Hi, nice one Ron. I tried this : expandcollapse popup#include <file.au3> #include <Array.au3> Global $pathOld = "c:\Downloads\AutoIt-Skripte\Entwicklung\ForumTests\TM_Software.txt" Global $path1 = "c:\Downloads\AutoIt-Skripte\Entwicklung\ForumTests\TM_Software_1.txt" Global $path2 = "c:\Downloads\AutoIt-Skripte\Entwicklung\ForumTests\TM_Software_2.txt" Global $path3 = "casdasd:\Downloads\AutoIt-Skripte\Entwicklung\ForumTests\TM_Software_3.txt" Global $search = "Windows" ConsoleWrite(_removeLineInFile($pathOld, $path1, $search, 1) & @CRLF) ConsoleWrite(_removeLineInFile($pathOld, $path2, $search, 2) & @CRLF) ConsoleWrite(_removeLineInFile($pathOld, $path3, $search, 3) & @CRLF) ;=============================================================================== ; ; Function Name: _removeLineInFile ; Description:: _removeLineInFile ; Parameter(s): Opt = 1 delete line if matches $search ; Opt = 2 Line starts with ; Opt = 3 Line contains ; Requirement(s): #include <file.au3> and #include <Array.au3> ; Return Value(s): 1 success ; Return Value(s): -1 file not found ; Return Value(s): -2 invalid path to write ; Author(s): th.meger ; ;=============================================================================== ; Func _removeLineInFile($pathOld, $pathNew, $search, $opt = 1) Local $FileOne Local $FileTwo[1] If Not _FileReadToArray($pathOld, $FileOne) Then Return -1 For $i = 1 To $FileOne[0] Switch $opt Case 1 If $FileOne[$i] <> $search Then _ArrayAdd($FileTwo, $FileOne[$i]) Case 2 If StringLeft($FileOne[$i], StringLen($search)) <> $search Then _ArrayAdd($FileTwo, $FileOne[$i]) Case 3 If StringInStr($FileOne[$i], $search) = 0 Then _ArrayAdd($FileTwo, $FileOne[$i]) EndSwitch Next _FileWriteFromArray($pathNew, $FileTwo, 1) If @error = 0 Then Return 1 Return -2 EndFunc ;==>_removeLineInFile So long, Mega Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times 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