Jump to content
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble

MSFN is made available via donations, subscriptions and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. Alternatively, register and become a site sponsor/subscriber and ads will be disabled automatically. 



Sign in to follow this  
nihylo

VBScript - Using a wildcard for a text-replacing script

Recommended Posts

nihylo    0

Hi,

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
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 :)

Share this post


Link to post
Share on other sites

CoffeeFiend    0

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×