Replace text

Posted on 2005-04-06
Last Modified: 2010-05-02
I am wanting to replace text in a file with a new value, has anyone got a script that could be show me what I need to do.

Its needs to open the file, replace the text and then save it as another name.

Question by:dwelldon
    LVL 17

    Accepted Solution

    This linke shows about files in script:

    Are you sure you wamt to use script becuase script does have some drawbacks. Which pure VB6 does not have.

    But here it is:

    To Read a file:

    Const ForReading = 1
    Const ForWriting = 2
    Dim objFSO
    Dim objFile
    Dim strCharacters
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objFile = objFSO.OpenTextFile("C:\MyFile.txt", ForReading)
    strCharacters = objFile.Read(10)

    strCharacters = Relpace(strCharacters, "Some String", "With String")

    Set objFile = objFSO.OpenTextFile("C:\MyFile.txt", ForWriting)
    objFile.Write strCharacters

    LVL 17

    Expert Comment

    So here is a tip about scripting the simple way.

    Use the VB6 .IDE set a reference to the Microsoft scripting object then you can start.
    Remove the form1 you get when you create a new project and add a module in the module create a Sub Main().
    Check the project properties start up to Sub Main()

    When you write you code you declare vaiables like this:

    Dim objFSO As Scripting.FileSystemObject

    Now when you write your code you just type "objFSO." (without the quotes) and you get a list of the methods and properties for the object making it easy to code for objects you do not know well.

    When the code is running, urn it back into scrpit but changing declarations with a comment '

    Dim objFSO  ' As Scripting.FileSystemObject

    (In VB Script you are not allowed Dim As. By adding the ' it disables the As bit so you can port back to VB if you need to rework the script.)

    The other problem is that you may not know what the values for constants are, for example ForReading.
    So in you VB you can say:

    MsgBox cStr(ForReading)

    And so you know what value to use in the Const declaration.

    e.g. Const ForReading = 1

    LVL 14

    Expert Comment

    by:Shiju Sasidharan
    or try this one
    'Place  a command button to ur form and use this code
    'Add reference
    'Project Menu -> References -> Microsoft Scripting Runtime

    Private Sub ReplaceAndSaveFile(ByVal SourceFile As String, ByVal DestFile As String, ByVal FindString As String, ByVal ReplaceString As String)
    On Error GoTo Last:
    Dim objFSO As New FileSystemObject
    Dim objTextStream As TextStream
    Dim sFileContent As String

        If Not objFSO.FileExists(SourceFile) Then
            MsgBox "Source File - " & SourceFile & " does not exist", vbExclamation
            GoTo Last
        End If
        Set objTextStream = objFSO.OpenTextFile(SourceFile, ForReading)
        sFileContent = objTextStream.ReadAll
        sFileContent = Replace(sFileContent, FindString, ReplaceString, 1)
        Set objTextStream = objFSO.OpenTextFile(DestFile, ForWriting, True)
        objTextStream.Write sFileContent
        MsgBox "New file- " & DestFile & " created", vbInformation
        If Err.Number <> 0 Then
            MsgBox Err.Description
        End If
    End Sub
    Private Sub Command1_Click()
         Call ReplaceAndSaveFile("C:\Source.txt", "C:\Dest.txt", "hello", "Hai")
    End Sub


    LVL 4

    Author Comment

    Thanks for all your help....

    Its working like a treat...

    The reason I want use a script is because I don't have VB6 environment.

    LVL 17

    Expert Comment

    Its a good investment.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    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…
    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…

    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

    20 Experts available now in Live!

    Get 1:1 Help Now