Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - VBScript: Search/Replace Issues

Reply
 
Old 10-12-2009   #1 (permalink)
Jen P.


 
 

VBScript: Search/Replace Issues

Hi,

I've written a VBScript that opens a text file, performs a search/
replace on a long list of strings, and saves the text file as a new
file.

I'm performing the Replace as follows:

::: code snippet :::

strNewText = Replace(strText, "&", "&", 1, -1, 1)
strNewText = Replace(strNewText, "&", "&", 1, -1, 1)
strNewText = Replace(strNewText, "&", "&", 1, -1, 1)
strNewText = Replace(strNewText, "‘", "'", 1, -1, 1)
strNewText = Replace(strNewText, "’", "'", 1, -1, 1)
strNewText = Replace(strNewText, "'", "'", 1, -1, 1)
strNewText = Replace(strNewText, "<div><div>","<p>", 1, -1, 1)
strNewText = Replace(strNewText, "<div>","<p>", 1, -1, 1)
strNewText = Replace(strNewText, "<p><p>","<p>", 1, -1, 1)
strNewText = Replace(strNewText, "<p> <p>","<p>", 1, -1, 1)

::: end code snippet :::

As I have 80+ strings to replace, I was wondering if there was a
different way to perform this operation. At times, my list changes and
some of these must be performed in a certain order.

I'm also running into issues where the double <p> tags are being
replaced with a single tag (as indicated in my second-to-last replace
string above); however, it doesn't seem to be replacing all of them
throughout the text file, even though I'm setting strText =
objFile.ReadAll. Would I need to loop this replace somehow?

This is the first script I've written in more than 10 years, so
believe me when I say I'm rusty. Any guidance is much appreciated.

Thanks,
jp

My System SpecsSystem Spec
Old 10-12-2009   #2 (permalink)
Tim Williams


 
 

Re: VBScript: Search/Replace Issues

Something like this (untested)
'*******************************
arrReplace = Array("&amp;amp;", "&", _
"&amp;", "&", _
"&lsquo", "'" ) 'etc....
strNew=strText

for x = 0 to ubound(arrReplace)-1
strNew=replace(strNew, arrReplace(x), arrReplace(x+1),1,-1,1)
next
'*******************************

If you still have double <p> remaining after your current script then maybe
you have input with 4 consecutive <p> ?

Tim


"Jen P." <jp78kt@newsgroup> wrote in message
news:77f847f9-cb16-43b5-8be9-eb044c386da9@newsgroup
Quote:

> Hi,
>
> I've written a VBScript that opens a text file, performs a search/
> replace on a long list of strings, and saves the text file as a new
> file.
>
> I'm performing the Replace as follows:
>
> ::: code snippet :::
>
> strNewText = Replace(strText, "&amp;amp;", "&", 1, -1, 1)
> strNewText = Replace(strNewText, "&", "&", 1, -1, 1)
> strNewText = Replace(strNewText, "&amp;", "&", 1, -1, 1)
> strNewText = Replace(strNewText, "&lsquo;", "'", 1, -1, 1)
> strNewText = Replace(strNewText, "&rsquo;", "'", 1, -1, 1)
> strNewText = Replace(strNewText, "'", "'", 1, -1, 1)
> strNewText = Replace(strNewText, "<div><div>","<p>", 1, -1, 1)
> strNewText = Replace(strNewText, "<div>","<p>", 1, -1, 1)
> strNewText = Replace(strNewText, "<p><p>","<p>", 1, -1, 1)
> strNewText = Replace(strNewText, "<p> <p>","<p>", 1, -1, 1)
>
> ::: end code snippet :::
>
> As I have 80+ strings to replace, I was wondering if there was a
> different way to perform this operation. At times, my list changes and
> some of these must be performed in a certain order.
>
> I'm also running into issues where the double <p> tags are being
> replaced with a single tag (as indicated in my second-to-last replace
> string above); however, it doesn't seem to be replacing all of them
> throughout the text file, even though I'm setting strText =
> objFile.ReadAll. Would I need to loop this replace somehow?
>
> This is the first script I've written in more than 10 years, so
> believe me when I say I'm rusty. Any guidance is much appreciated.
>
> Thanks,
> jp

My System SpecsSystem Spec
Old 10-13-2009   #3 (permalink)
Al Dunbar


 
 

Re: VBScript: Search/Replace Issues


"Jen P." <jp78kt@newsgroup> wrote in message
news:77f847f9-cb16-43b5-8be9-eb044c386da9@newsgroup
Quote:

> Hi,
>
> I've written a VBScript that opens a text file, performs a search/
> replace on a long list of strings, and saves the text file as a new
> file.
>
> I'm performing the Replace as follows:
>
> ::: code snippet :::
>
> strNewText = Replace(strText, "&amp;amp;", "&", 1, -1, 1)
> strNewText = Replace(strNewText, "&", "&", 1, -1, 1)
> strNewText = Replace(strNewText, "&amp;", "&", 1, -1, 1)
> strNewText = Replace(strNewText, "&lsquo;", "'", 1, -1, 1)
> strNewText = Replace(strNewText, "&rsquo;", "'", 1, -1, 1)
> strNewText = Replace(strNewText, "'", "'", 1, -1, 1)
> strNewText = Replace(strNewText, "<div><div>","<p>", 1, -1, 1)
> strNewText = Replace(strNewText, "<div>","<p>", 1, -1, 1)
> strNewText = Replace(strNewText, "<p><p>","<p>", 1, -1, 1)
> strNewText = Replace(strNewText, "<p> <p>","<p>", 1, -1, 1)
>
> ::: end code snippet :::
>
> As I have 80+ strings to replace, I was wondering if there was a
> different way to perform this operation. At times, my list changes and
> some of these must be performed in a certain order.
I would recommend re-writing your script to turn the series of calls to
replace into a loop. It would read the strings to replace (and what to
replace them with) from a text file, perhaps in csv format. Then when you
have to change the list, you are not modifying the actual script, just a
data file.
Quote:

> I'm also running into issues where the double <p> tags are being
> replaced with a single tag (as indicated in my second-to-last replace
> string above); however, it doesn't seem to be replacing all of them
> throughout the text file, even though I'm setting strText =
> objFile.ReadAll. Would I need to loop this replace somehow?
Have you inspected a typical html file to see what exactly makes up the
whitespace between doubled <p> tags? If there is a relatively limited number
of options, you could simply add them to your list. If not, you might get
somewhere with regular expressions, however this could be fairly
complicated.

/Al


My System SpecsSystem Spec
Old 10-13-2009   #4 (permalink)
Jen P.


 
 

Re: VBScript: Search/Replace Issues

On Oct 12, 11:43*pm, "Tim Williams" <timjwilli...@newsgroup> wrote:
Quote:

> Something like this (untested)
> '*******************************
> arrReplace = Array("&amp;amp;", "&", _
> * * * * * * * * * * * * * * * *"&amp;", "&", _
> * * * * * * * * * * * * * * * *"&lsquo", "'" ) 'etc....
> strNew=strText
>
> for x = 0 to ubound(arrReplace)-1
> * strNew=replace(strNew, arrReplace(x), arrReplace(x+1),1,-1,1)
> next
> '*******************************
Thanks for the guidance here. My loop is giving me fits - I have
managed to write only the last search/replace instance in the array.
If my logic is correct, the loop above should perform as follows:

Search for the string in the defined array, replace it with the next
string (hence the x+1) and loop through until the end of the array is
reached.

If I'm off-base here, please let me know? I've done some research and
thought I understood it, so I'm not sure what I'm missing here. Here's
the code snippet:

'*********************
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strRawFile, ForReading)

strText = objFile.ReadAll
objFile.Close

arrReplace = Array("&amp;amp;", "&", "&", "&", "&amp;", "&", "<!
[CDATA[", "", "]]>", "")
strNewText=strText

For x = 0 To UBound(arrReplace)-1
strNewText=Replace(strNewText, arrReplace(x),arrReplace(x+1),1,-1,1)
Next

Set objFile = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(strRawFile, ForWriting)
objFile.WriteLine strNewText

'*********************

I've also looked into calling a CSV file to perform the search/replace
as Al suggested. I'm trying to (re)learn as I go. Is calling the CSV
the same as importing the data from the CSV into an array, splitting
it and then performing the replace loop?

Thanks,
jp
My System SpecsSystem Spec
Old 10-14-2009   #5 (permalink)
Tim Williams


 
 

Re: VBScript: Search/Replace Issues

Sorry - I missed the "Step 2" from the for loop

for x = 0 to ubound(arrReplace)-1 Step 2
....
next

Tim

"Jen P." <jp78kt@newsgroup> wrote in message
news:815b3aa1-a015-4a72-bbc5-2a6da161a843@newsgroup
On Oct 12, 11:43 pm, "Tim Williams" <timjwilli...@newsgroup> wrote:
Quote:

> Something like this (untested)
> '*******************************
> arrReplace = Array("&amp;amp;", "&", _
> "&amp;", "&", _
> "&lsquo", "'" ) 'etc....
> strNew=strText
>
> for x = 0 to ubound(arrReplace)-1
> strNew=replace(strNew, arrReplace(x), arrReplace(x+1),1,-1,1)
> next
> '*******************************
Thanks for the guidance here. My loop is giving me fits - I have
managed to write only the last search/replace instance in the array.
If my logic is correct, the loop above should perform as follows:

Search for the string in the defined array, replace it with the next
string (hence the x+1) and loop through until the end of the array is
reached.

If I'm off-base here, please let me know? I've done some research and
thought I understood it, so I'm not sure what I'm missing here. Here's
the code snippet:

'*********************
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strRawFile, ForReading)

strText = objFile.ReadAll
objFile.Close

arrReplace = Array("&amp;amp;", "&", "&", "&", "&amp;", "&", "<!
[CDATA[", "", "]]>", "")
strNewText=strText

For x = 0 To UBound(arrReplace)-1
strNewText=Replace(strNewText, arrReplace(x),arrReplace(x+1),1,-1,1)
Next

Set objFile = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(strRawFile, ForWriting)
objFile.WriteLine strNewText

'*********************

I've also looked into calling a CSV file to perform the search/replace
as Al suggested. I'm trying to (re)learn as I go. Is calling the CSV
the same as importing the data from the CSV into an array, splitting
it and then performing the replace loop?

Thanks,
jp


My System SpecsSystem Spec
Old 10-14-2009   #6 (permalink)
Jen P.


 
 

Re: VBScript: Search/Replace Issues

On Oct 14, 2:16*am, "Tim Williams" <timjwilli...@newsgroup> wrote:
Quote:

> Sorry - I missed the "Step 2" from the for loop
>
> for x = 0 to ubound(arrReplace)-1 Step 2
> ...
> next
The step 2 is just what I needed to work with the flat array - thanks
to everyone for your assistance. My script now parses the text, writes
to a new file and outputs a log of changes.

Thanks again,
jp

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Binary search and replace General Discussion
Replace Start Menu Search with 3rd Party Search General Discussion
Advanced find and replace using VBScript VB Script
Search & Replace on a word doc PowerShell
search and replace string PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46