Search and Replace Text Strings
Keywords: BinaryPeekStr
Question:
How would I do a search and replace text string to a file?I know BinaryPokeStr can add a new line to a file, though I can't figure how replace a text string with a new string. ie., testfile.txt contains:
the dog is green the dog is blue the dog is blue the dog is red the dog is redHow would I replace all "the dog is red" lines with the "dog is red"? (exclude the "the" in the sentence?)Answer:
;=========Modify these variables as needed============== infind="the dog is red" outReplace="dog is red" infilename="C:\Temp\testfile.txt" outfilename="C:\Temp\testfileout.txt" ;================================================================== if fileexist(infilename)==0 message("Opps","File %infind% doesn't exist") exit endif insize=FileSize(infilename) inbuffer= BinaryAlloc(insize) BinaryRead(inbuffer, infilename) BinaryReplace(inbuffer, infind, outReplace, @FALSE) BinaryWrite(inbuffer,outfilename) BinaryFree(inbuffer) ;==================================================================Example of how to Replace CRLFs with LFs:
;=========Modify these variables as needed============== infind=@crlf outReplace=@lf infilename="C:\Temp\testfile.txt" outfilename="C:\Temp\testfileout.txt" ;================================================================== if fileexist(infilename)==0 message("Opps","File %infind% doesn't exist") exit endif insize=FileSize(infilename) inbuffer= BinaryAlloc(insize) BinaryRead(inbuffer, infilename) BinaryReplace(inbuffer, infind, outReplace, @FALSE) BinaryWrite(inbuffer,outfilename) BinaryFree(inbuffer)
Article ID: W12734Filename: Search and Replace Another Example.txt