Jump to content

unsure how to....


t0ddie
 Share

Recommended Posts

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...