I have a "search & replace within a file" script that works, but not as I expect. It has 2 problems:
- it changes the
oldtext
withnewtext
regadless if the old text has more strings after it: I want to avoid this situation!
i'm calling that scrip from a batch file that contains:
cscript replace.vbs file.txt hello world
presuming file.txt contains:
house
abc.hellowide.use
abc.hello.use
hello
hellonurse
I want the script to change the file into this:
house
abc.hellowide.use
abc.world.use
wolrd
hellonurse
for now, the scrip result unwanted changes like this:
house
abc.worldwide.use
abc.world.use
wolrd
worldnurse
i want that only the exact old text will be replaced.
- how can i make the script recurse subfolders under the root dir from where the script runs?
here is my script untill now:
Const ForReading = 1
Const ForWriting = 2
strFileName = Wscript.Arguments(0)
strOldText = Wscript.Arguments(1)
strNewText = Wscript.Arguments(2)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, strOldText, strNewText,1,1000,1)
Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.WriteLine strNewText
objFile.Close