Solved

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

Posted on 2013-12-02
2
494 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

726 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