Solved

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

Posted on 2013-12-02
2
493 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 25

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

738 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