Link to home
Start Free TrialLog in
Avatar of dwelldon
dwelldon

asked on

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.

DJW
ASKER CERTIFIED SOLUTION
Avatar of inthedark
inthedark
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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







hi
or try this one
'Place  a command button to ur form and use this code
'also
'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
    objTextStream.Close
    sFileContent = Replace(sFileContent, FindString, ReplaceString, 1)
    Set objTextStream = objFSO.OpenTextFile(DestFile, ForWriting, True)
    objTextStream.Write sFileContent
    objTextStream.Close
    MsgBox "New file- " & DestFile & " created", vbInformation
Last:
    If Err.Number <> 0 Then
        MsgBox Err.Description
        Err.Clear
    End If
End Sub
Private Sub Command1_Click()
    'Usage:
     Call ReplaceAndSaveFile("C:\Source.txt", "C:\Dest.txt", "hello", "Hai")
End Sub

'====================================================

;-)
Shiju
Avatar of dwelldon
dwelldon

ASKER

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.