Solved

VBS script to replace string values.

Posted on 2014-11-17
3
403 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
  • 2
3 Comments
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now