How to replace a string for another one in a file

Posted on 2011-10-25
Last Modified: 2012-05-12

I have a text file that contains a big string and I need replace part of it, something like this:

"This a big string inside a file" and I need to do this "This a modified string inside a file"

Thanks in advance
Question by:dimensionav
    LVL 24

    Expert Comment

    You can use Find and Replace?
    LVL 76

    Accepted Solution

    Any reason you just don't open the file, read the line and use a replace then write it back out?
    LVL 17

    Assisted Solution

    set FSO = CreateObject("Scripting.FileSystemObject")
    Dim fileStream
    Dim s
    Set fso =

    Set fileStream = fso.OpenTextFile("c:\workshop\File1.txt", 1)
    s = Replace(fileStream.ReadAll, "This a big string inside a file", "This a modified string inside a file")

    Set fileStream = fso.OpenTextFile("c:\workshop\File1.txt", 2)
    fileStream.Write (s)

    Author Comment


    The string that will be replaced is very specific and the new string is created by a query, actually I need to do some kind of automated "find and replace"
    LVL 76

    Expert Comment

    by:slightwv (䄆 Netminder)
    I'm not following why you cannot open file , read contents ,replace string, write new string, close file.

    wither using the code in the example I posted or snippet provided by aiklamha.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Suggested Solutions

    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    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…
    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…

    728 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

    16 Experts available now in Live!

    Get 1:1 Help Now