MSFN Forum: VBScript - Using a wildcard for a text-replacing script - MSFN Forum

Jump to content



Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

VBScript - Using a wildcard for a text-replacing script Rate Topic: -----

#1 User is offline   nihylo 

  • Group: Members
  • Posts: 4
  • Joined: 22-April 08

Posted 28 July 2008 - 08:57 AM

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


#2 User is offline   CoffeeFiend 

  • Coffee Aficionado
  • Group: Super Moderator
  • Posts: 5,036
  • Joined: 14-July 04
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 28 July 2008 - 09:11 AM

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

This post has been edited by crahak: 28 July 2008 - 09:48 AM


#3 User is offline   nihylo 

  • Group: Members
  • Posts: 4
  • Joined: 22-April 08

Posted 28 July 2008 - 01:11 PM

You're right, I'll better go with sed. Thanks.

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy