Solved

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

Posted on 2013-12-02
2
483 Views
Last Modified: 2013-12-03
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" />
0
Comment
Question by:JasonLattin
2 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 39691142
The offending line is your "If" condition. You see, the new string contains all characters of the old string, meaning that for the text with old string or with new string the condition "InStr(strText,strNew)" will always be "<> 0". I suggest you change the condition to this:
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,strOld) > 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

Open in new window

Also, remove the trailing spaces from the strOld (as I did in the code)
0
 

Author Closing Comment

by:JasonLattin
ID: 39692545
Worked like a charm! Thanks for the second set of eyes.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now