Avatar of JasonLattin
JasonLattin
 asked on

Simple Find/Replace text using VB script choking with special charachters.

I have a script that searches a specific file and  finds/replaces a string of text. I believe it is choking on some special charachters in that line of text.

I suspect the gremlin is in the assignment of the "strOld" variable.
I am missing quotes or something simple. I just can't see it.

Note that our script is finding the file correctly and is able to open it. It just can't find that specific line of txt. If I simplify it and just look for "pdfviewer", I can find/replace it with no problem. It is just choking on the full line with all of the special charachters.


Here is the section of vbs code I am struggling with. What am I missing?  :
-----------------
Function SearchReplaceFile(sTargetFileName)
      strOld = "   <!-- <viewer type=""Controls.PDFViewer, gui.viewers"" /> -->"
                  WScript.Echo "Looking for " + strOld
      strNew = "<viewer type=""Controls.PDFViewer, gui.viewers"" />"
                  WScript.Echo "Replacing with " + strNew
      Set objFile = oFSO.OpenTextFile(sTargetFileName, 1)
      strText = objFile.ReadAll
      objFile.Close
      If ( InStr(strText,strNew) = 0 ) Then
            WScript.Echo "Found text to replace in " + sTargetFileName
            strNewText = Replace(strText,strOld,strNew)
            Set objFile = oFSO.OpenTextFile(sTargetFileName, 2)
            objFile.WriteLine strNewText
            objFile.Close
      Else
            WScript.Echo "Didn't find text to replace in " + sTargetFileName
      End If
End Function


-----------------

The actual line we are looking for is :
    <!-- <viewer type="Controls.PDFViewer, gui.viewers" /> -->

We want to replace it with :
    <viewer type="Controls.PDFViewer, gui.viewers" />
VB ScriptVisual Basic ClassicVisual Basic.NET

Avatar of undefined
Last Comment
JasonLattin

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
chaau

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
JasonLattin

ASKER
Worked like a charm! Thanks for the second set of eyes.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23