Jofnn
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
COMMAND LINE
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
COMMAND LINE
cscript TheScript.vbs "thefilename.txt" "the_old_value" "the_new_value"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
I've tested it and it works perfectly for what I need!!
Thanks again!
Thanks again!
ASKER
Perfect!
Great, glad that worked out, thanks for the feedback.
~bp
~bp
~bp