t0ddie Posted September 13, 2004 Share Posted September 13, 2004 is there an easy example that can be shown to "find" a variable in a string, and delete the entire thing? i want to make an array, and search through variables. but.. i want to remove the entire line the variable is in. im stuck. for example, i have test.txt on my desktop, and in that file are 6 lines c:\funtime\good\ERASE\ c:\yes\no\maybe\ c:\fool\fun\ferrets\ c:\ERASE\left\right\center\ c:\yahoo\google\ c:\d\e\f\g\ i want to basically, find any line that has "ERASE" in it, and delete the whole line. so i should end up with this. c:\yes\no\maybe\ c:\fool\fun\ferrets\ c:\yahoo\google\ c:\d\e\f\g\ thanks for your help in advance ~Todd Valik Note Added 19 October 2006 - 08:38 AMAdded to warn level I just plain don't like you. Link to comment Share on other sites More sharing options...
Billy Posted September 13, 2004 Share Posted September 13, 2004 One way to handle this is to make a "duplicate" array and place into it all of the strings you want to keep. This makes the "duplicate" array larger than you need, but there is no dynamic resizing of arrays in AutoIt. Therefore, as you read the duplicate array, test for empty values which mean you're at the end of the list or file, if you will. You also do not have to have a duplicate array. If you're just looking to make a "clean" file (one without strings containing a specified substring), just change the line where the second array is updated with a FileWriteLine. #include <File.au3> ; for a very nice function for reading files Dim $I, $Count ; counters Dim $TxtArray1, $TxtArray2 ; an "in" array and an "out" array ; this creates the "in" array If _FileReadToArray("C:\Test\TestFile.txt", $TxtArray1) = 0 Then Exit ; (do something nicer than just Exit. this is just a tutorial) ; this creates the "out" array Dim $TxtArray2[Ubound($TxtArray1)-1]; (the way Ubound works, you have to subtract one) For $I = 1 to Ubound($TxtArray1)-1 ; (ditto above) If StringInStr($TxtArray1[$I], "ERASE") = 0 Then ; if the line contains "ERASE" then keep it $Count = $Count + 1 ; bump up the "out" counter $TxtArray2[$Count] = $TxtArray1[$I] ; store the value in the "out" array MsgBox(0,"Debug", $TxtArray2[$Count]) ; if you want to see it as it happens Else ; the line contained "ERASE" so we skip storing it ; (you don't need this line or the Else above) EndIf Next Link to comment Share on other sites More sharing options...
Valik Posted September 13, 2004 Share Posted September 13, 2004 but there is no dynamic resizing of arrays in AutoIt.ReDim will resize an array and preserve elements. Link to comment Share on other sites More sharing options...
t0ddie Posted September 13, 2004 Author Share Posted September 13, 2004 cool. is there a way to do it with the #include command if i dont have the list before compilation? what i mean is... i have a script that makes this list, and i just want to check these variables (lines) as it makes the list. then if possible, just not write them into the list in the first place. i can thank larry for the code i have so far so, the script makes the list... and i cant "#include" it if its not made yet, right? well, heres the code, thanks to larry $Stack = "c:," $FileList = "c:\," While $Stack <> "" $root = StringLeft($Stack,StringInStr($Stack,",")-1) & "\" $Stack = StringTrimLeft($Stack,StringInStr($Stack,",")) $h = FileFindFirstFile($root & "*.*") If $h > -1 Then $FileFound = FileFindNextFile($h) While Not @Error And $FileFound <> "" If $FileFound <> "." And $FileFound <> ".." And _ StringInStr(FileGetAttrib($root & $FileFound),"D") Then $Stack = $Stack & $root & $FileFound & "," $FileList = $FileList & $root & $FileFound & "\" & @CRLF EndIf $FileFound = FileFindNextFile($h) Wend FileClose($h) EndIf Wend FileWrite("test.txt",$FileLIst) can what i want be incorporated into this? so that if it finds a directory with "ERASE" in it, it will omit it from the list. thanks awholehellofalot and thanks AGAIN for your continued support. ~Todd Valik Note Added 19 October 2006 - 08:38 AMAdded to warn level I just plain don't like you. Link to comment Share on other sites More sharing options...
Billy Posted September 14, 2004 Share Posted September 14, 2004 Whoops! My bad for missing ReDim. I looked too (just in the wrong place). To exclude directories with ERASE in their name and using your code you could surround the concatination of $FileList with a condition. If StringInStr($FileList & $root & $FileFound, "ERASE") = 0 Then $FileList = $FileList & $root & $FileFound & "\" & @CRLF EndIf That is, if ERASE is not found in the string, then go ahead and add to $FileList. Otherwise, you just skip it. You wouldn't use includes because they are for inserting code only, not plain text. 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