Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Find and Replace Multiple Values

Posted on 2010-08-25
4
Medium Priority
?
1,025 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Loops Section Overview
Suggested Courses

971 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