We help IT Professionals succeed at work.

VBS file to handle wildcard character in find/replace

Jofnn
Jofnn asked
on
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

Comment
Watch Question

Test your restores, not your backups...
SILVER EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
Okay, I think your best bet to do that type of thing is to leverage regular expressions in the VBS script.  Here's an updated version that takes advantage of these, and can do what you want with the following command line:

cscript EE27425934.vbs in.txt "PIZZA_." "PIZZA_L"

More info on VBS regex support here:

http://msdn.microsoft.com/en-us/library/ms974570.aspx

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

' Replace desired string
Set objRegExp = New RegExp
objRegExp.Global = True
objRegExp.IgnoreCase = False
objRegExp.Pattern = strOriginal
strReplacement = objRegExp.Replace(strText, strReplacement)

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

Open in new window

~bp
Bill PrewTest your restores, not your backups...
SILVER EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
(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

Author

Commented:
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.

Author

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

Thanks again!

Author

Commented:
Perfect!
Bill PrewTest your restores, not your backups...
SILVER EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Great, glad that worked out, thanks for the feedback.

~bp