?
Solved

VBS script to replace string values.

Posted on 2014-11-17
3
Medium Priority
?
534 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 56

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 56

Accepted Solution

by:
Bill Prew earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

752 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