Help - Search - Members - Calendar
Full Version: VBScript - Using a wildcard for a text-replacing script
MSFN Forums > Coding, Scripting and Servers > Programming (C++, Delphi, VB, etc.)

   
Google Internet Forums Unattended CD/DVD Guide
nihylo
Hi,

I'm trying to use a wildcard for my text-replacing script but unfortunately, all of my previous attempts failed...

CODE
Const ForReading = 1
Const ForWriting = 2

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

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "tutu", "tata")

Set objFile = objFSO.OpenTextFile("bla.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close


I just want to specify "*.txt" rather than "bla.txt"

Thanks by advance smile.gif
crahak
The OpenTextFile method only opens one file (returns 1 file handle), so using a wildcard there doesn't even make sense.

If you want to process more than one file, you'll have to enumerate them first, and then process them one by one.

Also, just wondering why you're even doing this. Unless you have very specific needs that would be solved by a specialized app or script, there's no point in wasting time reinventing the wheel poorly. The "text replace in files" problem has been mostly solved since pretty much forever, using standard utils like sed (which has been around for 30+ years).

What you seem to want to do (replace "tutu" by "tata" in *.txt) is trivial to do using sed:
CODE
sed -i "s/tutu/tata/g" *.txt

All done...

-i -> edit the files (not make copies)
s -> substitute
g -> global
nihylo
You're right, I'll better go with sed. Thanks.
Google Internet Forums Unattended CD/DVD Guide
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.