Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Find and Replace Multiple Values

Posted on 2010-08-25
4
Medium Priority
?
1,021 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
soostibi earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

722 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