Link to home
Start Free TrialLog in
Avatar of Jofnn
JofnnFlag for United Kingdom of Great Britain and Northern Ireland

asked on

VBS file to handle wildcard character in find/replace

Hi,

I have a vbs script which I run via the command line with 3 arguments... This script is to simply find/replace a certain term within the text file... which I primarily use for dates.

I want to be able to amend the script to use a wildcard to represent one character at a time.  So if I was using it to search for "PIZZA_1" "PIZZA_A" "PIZZA_B" and replace them all with "PIZZA_L" - I could search for "PIZZA_*" where * represented ONE character.

Does that make sense?

VBS
Const ForReading = 1
Const ForWriting = 2

strName = Wscript.Arguments(0)
strOriginal = Wscript.Arguments(1)
strReplacement = Wscript.Arguments(2)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strName, ForReading)

strText = objFile.ReadAll
objFile.Close
strReplacement = Replace(strText, strOriginal, strReplacement)

Set objFile = objFSO.OpenTextFile(strName, ForWriting)
objFile.WriteLine strReplacement
objFile.Close

Open in new window


COMMAND LINE
cscript TheScript.vbs "thefilename.txt" "the_old_value" "the_new_value"

Open in new window

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
Avatar of Bill Prew
Bill Prew

(note too that in the code I provided, I left it as case sensitive, not sure what you wanted, but you can see the value in the VBS to change I'm sure...)

~bp
Avatar of Jofnn

ASKER

Thanks Bill - I'll have a look.

The main use (initially) is actually for date changing in format YYYY-MM-DD within a file... It's so that we can change these all at once by using a wildcard (where required)...

i.e. 2000-01-** would replace any start with 2000-01-.  But, it needs to only handle the set amount of characters, rather than trying to replace other text that's attached to it.
Avatar of Jofnn

ASKER

I've tested it and it works perfectly for what I need!!

Thanks again!
Avatar of Jofnn

ASKER

Perfect!
Great, glad that worked out, thanks for the feedback.

~bp