Script to Replace Text in a file

Posted on 2012-09-12
Last Modified: 2012-09-14
Need a little help. I have a vbscript that will open up a file extension and change "Program Files" text to "Program Files (x86)". The problem is that if "Program Files (x86)" already exist in the file the script is apending and changing to  "Program Files (x86) (x86)". Trying to find a way to have the script look for "Program Files (x86)" and if exist do nothing, but continue to change "Program Files" to "Program Files (x86)". Here is the script:

Question by:jspade_69
    LVL 39

    Expert Comment

    Can you show your text file?
    May be there is something like Program Files\ and Program Files (x86)\?
    LVL 29

    Accepted Solution

    1 - What happens if you change

       strNewText = Replace(strText, "Program Files", "Program Files (x86)")

       strNewText = Replace(strText, "Program Files\", "Program Files (x86)\")

    2 - If this is a quick and dirty one shot, consider to replace
       strNewText = Replace(strText, "Program Files", "Program Files (x86)")
       strNewText = Replace(strText, "Program Files", "Program Files (x86)")
       strNewText = Replace(strText, "Program Files (x86)  (x86)", "Program Files (x86)")

    Author Comment

    Thanks're 1st suggestion worked like a simple yet so affective!   By the way i originally tried the 2nd option before posting and it didn't work.

    Thanks again
    LVL 29

    Expert Comment

    B-) Glad it worked and I could help. Thx for the grade and points!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Suggested Solutions

    Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
    Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    737 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

    15 Experts available now in Live!

    Get 1:1 Help Now