replace statement
hi,
i have a little problem,
i have a txt file and in that file i want replace spaces into zeros...
but can i say where to begin with replacing and where to stop?
this is my code
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim FileName, Find, ReplaceWith, FileContents, dFileContents
Find = " "
ReplaceWith = "000"
FileName = "\\srvbo\temp\KSTAYHOSP.TXT"
'Read source text file
FileContents = GetFile(FileName)
'replace all string In the source file
dFileContents = Replace(FileContents, Find, ReplaceWith, 1, -1, 1)
'Compare source And result
If dFileContents <> FileContents Then
'write result If different
WriteFile(FileName, dFileContents)
MsgBox("Replace done.")
If Len(ReplaceWith) <> Len(Find) Then 'Can we count n of replacements?
MsgBox((Len(dFileContents) - Len(FileContents)) / (Len(ReplaceWith) - Len(Find)) & " replacements.")
End If
Else
MsgBox("Searched string Not In the source file")
End If
End Sub
'Read text file
Function GetFile(ByVal FileName)
If FileName <> "" Then
Dim FS, FileStream
FS = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
FileStream = FS.OpenTextFile(FileName)
GetFile = FileStream.ReadAll
End If
End Function
'Write string As a text file.
Function WriteFile(ByVal FileName, ByVal Contents)
Dim OutStream, FS
On Error Resume Next
FS = CreateObject("Scripting.FileSystemObject")
OutStream = FS.OpenTextFile(FileName, 2, True)
OutStream.Write(Contents)
End Function
|