Solved

Find and Replace Multiple Values

Posted on 2010-08-25
4
1,004 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

16 Experts available now in Live!

Get 1:1 Help Now