Solved

Find and Replace Multiple Values

Posted on 2010-08-25
4
1,002 Views
Last Modified: 2012-05-10
Okay, at it again -
I have a very simple .vbs to find and replace one string; however, I would like to hard code multiple values (or build in a user input dialouge to ask user for next find and replace string) into one script.  This is what I have currently:
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Scripts\123.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "043487835", "04-3487835")
Set objFile = objFSO.OpenTextFile("C:\Scripts\123.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close

Open in new window

I would like to add another item to change within the same file (123.txt), but if I simply add another line for the next search and replace entry, it doesn't work (see example of non-working script below):
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Scripts\123.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "043487835", "04-3487835")
strNewText = Replace(strText, "5551234", "5551235")
Set objFile = objFSO.OpenTextFile("C:\Scripts\123.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close

Open in new window

I'm thinking maybe a loop, or a next statement, but again, I'm still learning so hopefull someone else can assist.
0
Comment
Question by:PMGIT
  • 2
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
soostibi earned 500 total points
ID: 33525300
I'm a PowerShell guy, but I think here is the problem in line 8 (second script):
strNewText = Replace(strNewText, "5551234", "5551235")
0
 

Author Comment

by:PMGIT
ID: 33525737
Hmmm, that worked interestingly enough. So if I were to add additional search and replace strings, would I have to do something else, or simply add the New moniker to each additional? I guess I don't understand how adding New to that element works :).
0
 
LVL 13

Expert Comment

by:soostibi
ID: 33526287
You replaced both numbers in the original text, so the result of the first replace was neglected.
The second replace should be performed on the result of the first, so in the final output you'll get the result of the combined two replaces.
0
 

Author Comment

by:PMGIT
ID: 33526587
Ohhhh, okay I think I've got it now. I was essentially overwriting the first set of changes with the second because I wasn't saying replace the "strNewText" rather, I was saying replace the 'strText' which was the original modification (I think).  According to this theory then, I should be able to continue on with as many 'find and replace' statements as I want by using the "strNewText" statements, correct...?  I did try it, and it works, I just wanted to make sure my logic is correct, and that I shouldn't be changing the third iteration to "strNewText2" or something.

 
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now