![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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, "&amp;", "&", 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 Specs![]() |
| | #2 (permalink) |
| | Re: VBScript: Search/Replace Issues Something like this (untested) '******************************* arrReplace = Array("&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;", "&", 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 Specs![]() |
| | #3 (permalink) |
| | 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;", "&", 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. 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? 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 Specs![]() |
| | #4 (permalink) |
| | Re: VBScript: Search/Replace Issues On Oct 12, 11:43*pm, "Tim Williams" <timjwilli...@newsgroup> wrote: Quote: > Something like this (untested) > '******************************* > arrReplace = Array("&amp;", "&", _ > * * * * * * * * * * * * * * * *"&", "&", _ > * * * * * * * * * * * * * * * *"&lsquo", "'" ) 'etc.... > strNew=strText > > for x = 0 to ubound(arrReplace)-1 > * strNew=replace(strNew, arrReplace(x), arrReplace(x+1),1,-1,1) > next > '******************************* 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;", "&", "&", "&", "&", "&", "<! [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 Specs![]() |
| | #5 (permalink) |
| | 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;", "&", _ > "&", "&", _ > "&lsquo", "'" ) 'etc.... > strNew=strText > > for x = 0 to ubound(arrReplace)-1 > strNew=replace(strNew, arrReplace(x), arrReplace(x+1),1,-1,1) > next > '******************************* 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;", "&", "&", "&", "&", "&", "<! [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 Specs![]() |
| | #6 (permalink) |
| | 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 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 Specs![]() |
![]() |
| 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 | |||