Link to home
Start Free TrialLog in
Avatar of williamfl
williamfl

asked on

VBS script to replace string values.

Need help with following script below. It works when i use a simple string value such a "bob" with "bill". such as

cscript replace.vbs "C:\Scripts\Text.txt" "bob " "bill "

What is needed to replace mutiple string Values.   Such as.
cscript //Nologo replace.vbs C:\Scripts\testA\data.xml "//Server123A:1726" "//Server345A:444"
cscript //Nologo replace.vbs C:\Scripts\testB\data.xml "//Server123B:1726" "//Server345B:444"

I need to batch the replace file process.

When we run i get input error.  We know it is the sting value. I tried to escape backslash  but did not work.
We have hundreds of files that have values in them we need to replace. The difficult part they are all unique string one value to new value.

Here is sample code

Replace.vbs

Find and replace a text string in a file.

'usage: cscript replace.vbs Filename "StringToFind" "stringToReplace"
 
Option Explicit
Dim fso,strFilename,strSearch,strReplace,objFile,oldContent,newContent
 
strFilename=WScript.Arguments.Item(0)
strSearch=WScript.Arguments.Item(1)
strReplace=WScript.Arguments.Item(2)
 
'Does file exist?
Set fso=CreateObject("Scripting.FileSystemObject")
if fso.FileExists(strFilename)=false then
   wscript.echo "file not found!"
   wscript.Quit
end if
 
'Read file
set objFile=fso.OpenTextFile(strFilename,1)
oldContent=objFile.ReadAll
 
'Write file
newContent=replace(oldContent,strSearch,strReplace,1,-1,0)
set objFile=fso.OpenTextFile(strFilename,2)
objFile.Write newContent
objFile.Close

Open in new window



Example

cscript //Nologo replace.vbs c:\docs\demo.txt "Madonna" "Lady Gaga"
Avatar of Bill Prew
Bill Prew

Yes, that's sort of a known "bug" in cscript, it doesn't honor the double quotes around parameter strings, and continues to search for switches in them, which start with //.  I'm not aware of a way to work around this from the command line.

I could suggest a couple of options if these could work, but they would require changing the replace.vbs script.

One approach would be to just place the replace pairs right in the replace.vbs script and then just run it.  It changes replace,vbs from general purpose to single purpose, so probably rename it to something else.

Another approach could be to read the pair values from a text file in the replace.vbs.  Perhaps a parameter passed to it with an @ sign in front could tell the program to read from a TXT file, this way we keep it general purpose working as it does now, but expanding it's capability to also be able to process the replace strings from a file.

Thoughts?

~bp
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Because the Cscript.exe options are preceded by double forward slashes, you cannot use them as input parameter other than the following:

 //B
 //D
 //E:engine
 //H:CScript
 //H:WScript
 //I
 //Job:xxxx
 //Logo
 //Nologo
 //S
 //T:nn
 //X
 //U

So to circumvent this, you can try only one slash in the Search and Replace strings as follows:


cscript //Nologo replace.vbs C:\Scripts\testA\data.xml "/Server123A:1726" "/Server345A:444"
cscript //Nologo replace.vbs C:\Scripts\testB\data.xml "/Server123B:1726" "/Server345B:444"