WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Binary Functions

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

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 red

How 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("Oh No!","File %infilename% 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:   W12734
Filename:   Search and Replace Another Example.txt
File Created: 2018:09:09:09:28:12
Last Updated: 2018:09:09:09:28:12