Replace text

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.

Who is Participating?
inthedarkConnect With a Mentor Commented:
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

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

Shiju SasidharanAssoc Project ManagerCommented:
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


dwelldonAuthor Commented:
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.

Its a good investment.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.