Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Regular Expression find and replace

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,1975
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Regular Expression find and replace
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Regular Expression find and replace - 2/3/2005 8:18:16 PM   
  tonyisthedude

 

Posts: 4
Score: 0
Joined: 2/3/2005
From:
Status: offline
I would like to do a find and replace in a text file and output the results to a dfferent output textfile.

I've found this code from hq_giap, but i cant figure out how to output results to an external text file:

----------------------------------------------------------

      
-----------------------------------------

Input file looks like this

john@company1.com
alai@company2.ca
hahs@company3.uk
john@company4.com
alai@company5.ca
hahs@company6.uk
alai@company7.ca
hahs@company8.uk
alai@company9.ca
 
 
Post #: 1
 
 Re: Regular Expression find and replace - 2/3/2005 8:48:11 PM   
  Zifter


Posts: 318
Score: 0
Joined: 1/5/2005
From: Belgium
Status: offline
The scripting guys have a column about how to save output to a text file.
You can read it here

<edit>Actually this article will suit your needs better...</edit>

HTH

(in reply to tonyisthedude)
 
 
Post #: 2
 
 Re: Regular Expression find and replace - 2/3/2005 8:55:21 PM   
  tonyisthedude

 

Posts: 4
Score: 0
Joined: 2/3/2005
From:
Status: offline
woohoo! here is my end result.

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, fsox, fx

namefile = inputbox("Enter the path & filename.")

Dim objRe, objMatches, outputFile

outputFile = "dumb_computers.txt" 'output file
Set fsox = CreateObject("Scripting.FileSystemObject")
Set fx = fsox.OpenTextFile(outputFile, ForWriting, True) 'opens output file

Set objRe = New RegExp 'Regular expression

objRe.Pattern = ".* Your location is " 'sets regular expression search pattern
objRe.Global = True
objRe.IgnoreCase = True 'caps sensitivity

set fso = createobject("scripting.filesystemobject")
set file_opened = fso.opentextfile (namefile) 'opens file from input

do
replaced = objRe.Replace(file_opened.readline,"")
fx.WriteLine(replaced)

loop until file_opened.AtEndOfStream
wscript.echo "done"

(in reply to tonyisthedude)
 
 
Post #: 3
 
 Re: Regular Expression find and replace - 2/3/2005 9:29:06 PM   
  Zifter


Posts: 318
Score: 0
Joined: 1/5/2005
From: Belgium
Status: offline
Just a tip; you don't have to create two FileSystemObjects. You could open both files with only one.
And I would suggest to specify the ForReading constant when opening the second file.

      
Not sure about the regular expression though...

HTH

(in reply to tonyisthedude)
 
 
Post #: 4
 
 Re: Regular Expression find and replace - 2/3/2005 10:21:39 PM   
  tonyisthedude

 

Posts: 4
Score: 0
Joined: 2/3/2005
From:
Status: offline
looks much cleaner now, thanks hehe

(in reply to tonyisthedude)
 
 
Post #: 5
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Regular Expression find and replace Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts