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
Its needs to open the file, replace the text and then save it as another name.
DJW
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(SourceFi le) Then
MsgBox "Source File - " & SourceFile & " does not exist", vbExclamation
GoTo Last
End If
Set objTextStream = objFSO.OpenTextFile(Source File, ForReading)
sFileContent = objTextStream.ReadAll
objTextStream.Close
sFileContent = Replace(sFileContent, FindString, ReplaceString, 1)
Set objTextStream = objFSO.OpenTextFile(DestFi le, 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:\Sou rce.txt", "C:\Dest.txt", "hello", "Hai")
End Sub
'========================= ========== ========== =======
;-)
Shiju
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(SourceFi
MsgBox "Source File - " & SourceFile & " does not exist", vbExclamation
GoTo Last
End If
Set objTextStream = objFSO.OpenTextFile(Source
sFileContent = objTextStream.ReadAll
objTextStream.Close
sFileContent = Replace(sFileContent, FindString, ReplaceString, 1)
Set objTextStream = objFSO.OpenTextFile(DestFi
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:\Sou
End Sub
'=========================
;-)
Shiju
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 working like a treat...
The reason I want use a script is because I don't have VB6 environment.
Its a good investment.
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