Flashz Posted March 25, 2005 Posted March 25, 2005 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: Quote $file = FileOpen("test.txt", 0)While 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop If StringinsStr ($line, "xxx") Then StringReplace ( $line, "xxx", "yyy" ) EndifWendFileClose($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.
benched42 Posted March 25, 2005 Posted March 25, 2005 Flashz said: 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?
Flashz Posted March 25, 2005 Author Posted March 25, 2005 benched42 said: $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.
Flashz Posted March 25, 2005 Author Posted March 25, 2005 Thanks for the reply. I managed to figure it out. Below is the code: Quote $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 WendFileClose($filein)FileClose($fileout)FileDelete("existing.txt")FileMove("newfile.txt", "existing.txt")ExitBtw, is there another way to do this replacement without having to create another file then renaming it back to the original one?
sandyd Posted March 25, 2005 Posted March 25, 2005 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 ]---
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