troubleshooting Question

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

Avatar of JasonLattin
JasonLattin asked on
Visual Basic ClassicVisual Basic.NETVB Script
2 Comments1 Solution522 ViewsLast Modified:
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
      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
            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" />
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros