Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account


VBScript - Using a wildcard for a text-replacing script

- - - - -

  • Please log in to reply
2 replies to this topic


  • Member
  • 4 posts
  • Joined 22-April 08

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

How to remove advertisement from MSFN



    Coffee Aficionado

  • Patrons
  • 4,973 posts
  • Joined 14-July 04
  • OS:Windows 7 x64
  • Country: Country Flag
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, 28 July 2008 - 09:48 AM.

Coffee: \ˈkȯ-fē, ˈkä-\. noun. Heaven in a cup. Life's only treasure. The meaning of life. Kaffee ist wunderbar. C8H10N4O2 FTW.


  • Member
  • 4 posts
  • Joined 22-April 08
You're right, I'll better go with sed. Thanks.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users