Sign in to follow this  
Followers 0

VBScript - Using a wildcard for a text-replacing script

3 posts in this topic


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

Const ForReading = 1
Const ForWriting = 2

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

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

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

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

Thanks by advance :)


Share this post

Link to post
Share on other sites

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:

sed -i "s/tutu/tata/g" *.txt

All done...

-i -> edit the files (not make copies)

s -> substitute

g -> global

Edited by crahak

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.