Jump to content
Sign in to follow this  
Flashz

FileRead and Replace String

Recommended Posts

Flashz

Is there a way to read through a file line by line and when I find a specific substring, I replace it with something else?

I tried to write a script that goes something like this:

$file = FileOpen("test.txt", 0)

While 1

    $line = FileReadLine($file)

    If @error = -1 Then ExitLoop

    If StringinsStr ($line, "xxx") Then

        StringReplace ( $line, "xxx", "yyy" )

    Endif

Wend

FileClose($file)

I do realise I might need to open the file with option 1 for write mode. But how do I read and at the same time replace all occurences of that string in the open file?

Any replies will be greatly appreciated.

Share this post


Link to post
Share on other sites
benched42

Is there a way to read through a file line by line and when I find a specific substring, I replace it with something else?

I tried to write a script that goes something like this:

I do realise I might need to open the file with option 1 for write mode. But how do I read and at the same time replace all occurences of that string in the open file?

Any replies will be greatly appreciated.

<{POST_SNAPBACK}>

You need to open the existing file to read and open a second, dummy file for writing. Then read from your existing file and populate your new file as you read the lines. After all is done, back up your existing file and rename the dummy file to your original file name. Sort of like this:

$filein = FileOpen("existing.txt",0)
$fileout = FileOpen("newfile.txt",2)

While 1
  $line = FileReadLine($filein)
  If @error = -1 Then ExitLoop
  If StringInStr($line,"xxx") Then
    StringReplace($line,"xxx","yyy")
  Endif
  FileWriteLine($fileout,$line)
Wend

FileClose($filein)
FileClose($fileout)

FileCopy($filein, $filein&".old",1)
FileCopy($fileout,$filein,1)
FileDelete($fileout)

Who lied and told you life would EVER be fair?

Share this post


Link to post
Share on other sites
Flashz

$filein = FileOpen("existing.txt",0)
$fileout = FileOpen("newfile.txt",2)

While 1
  $line = FileReadLine($filein)
  If @error = -1 Then ExitLoop
  If StringInStr($line,"xxx") Then
    StringReplace($line,"xxx","yyy")
  Endif
  FileWriteLine($fileout,$line)
Wend

FileClose($filein)
FileClose($fileout)

FileCopy($filein, $filein&".old",1)
FileCopy($fileout,$filein,1)
FileDelete($fileout)

<{POST_SNAPBACK}>

I tried this out but it's only giving me the same file back. Even though you are doing the validatoin for the "xxx" String, it's writing to $fileout after the if statement.

I tried placing the FileWrite inside the IF block, hoping it will at least give me only lines containing "xxx" that have been replaced by "yyy". But nothing is working yet.

Any other ideas? It looks so simple to do and yet I can't find where I'm going wrong.

Share this post


Link to post
Share on other sites
Flashz

Thanks for the reply. I managed to figure it out. Below is the code:

$filein = FileOpen("existing.txt",0)

$fileout = FileOpen("newfile.txt",2)

While 1

  $line = FileReadLine($filein)

If @error = -1 Then ExitLoop

  If StringInStr($line,"xxx") Then

    $line1 = StringReplace($line,"xxx","yyy")

FileWriteLine($fileout,$line1)

Else

FileWriteLine($fileout,$line)

Endif

Wend

FileClose($filein)

FileClose($fileout)

FileDelete("existing.txt")

FileMove("newfile.txt", "existing.txt")

Exit

Btw, is there another way to do this replacement without having to create another file then renaming it back to the original one?

Share this post


Link to post
Share on other sites
sandyd

The only way is to read the whole file into an array, do the processing on the array, then write the file back out again (overwriting the original)

Personally, I'd keep the original in case of any problems.


----[ SandyD ]---

Share this post


Link to post
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
Sign in to follow this  

×