Solved

Find and Replace Multiple Values

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

821 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