Solved

VBS script to replace string values.

Posted on 2014-11-17
3
436 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 52

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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

896 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

17 Experts available now in Live!

Get 1:1 Help Now