• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

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
0
dwelldon
Asked:
dwelldon
  • 3
1 Solution
 
inthedarkCommented:
This linke shows about files in script:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/sgworkingwithfiles.asp

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)
objFile.Close

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

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

0
 
inthedarkCommented:
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







0
 
Shiju SasidharanCommented:
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
0
 
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.

0
 
inthedarkCommented:
Its a good investment.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now