Solved

VBS script to replace string values.

Posted on 2014-11-17
3
491 Views
Last Modified: 2014-11-24
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"
0
Comment
Question by:williamfl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 54

Expert Comment

by:Bill Prew
ID: 40448877
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
0
 
LVL 54

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 40448916
Here's an updated version of replace.vbs that shows the second approach I mentioned above.  Using the files attached, run it like this:

cscript //nologo replace.vbs data.xml @control.txt

~bp
replace.vbs
control.txt
0
 
LVL 12

Expert Comment

by:jkaios
ID: 40449484
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"
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question