Editing text file

  • Thread starter Thread starter Foss
  • Start date Start date
F

Foss

Mornin' all,

I need to open a text file, then replace all occurences of
certain symbols with either nothing or a line return.

I really don't know where to start on this one, has anyone
got some pointers or code that would help me?

Thanks very much,
Foss
 
Hi Foss,
If that can help you:

Sub UpDateTxtFile()
Dim FSO As Object, File As Object, Contents As String
Set FSO = CreateObject("Scripting.FileSystemObject")

' Create file to test example:
Set File = FSO.CreateTextFile("c:\examplefile.txt")
Contents = "A quick example of the ReadAll method !!!"
Contents = Contents & vbCrLf
Contents = Contents & "New line for this example !!!"
File.Write Contents
File.Close

' Replacement routine
Const TxtFullPath As String = "c:\examplefile.txt"
Dim ReadFile As Object
Set ReadFile = FSO.OpenTextFile(TxtFullPath, 1, False)
Contents = ReadFile.ReadAll
ReadFile.Close: Set ReadFile = Nothing
With CreateObject("VBScript.RegExp")
.Global = True
.IgnoreCase = False
.Pattern = "[!" & vbCrLf & "]"
Contents = .Replace(Contents, "")
End With
Set File = FSO.CreateTextFile(TxtFullPath)
File.Write Contents
File.Close
Set File = Nothing: Set FSO = Nothing
End Sub

Regards,
MP
 
Hi there MP

Thanks, that's great! One more question though, how can I replace a space with a linefeed

I've tried using the Chr(10) thing but it doesn't seem to work

Cheers
Fos


----- Michel Pierron wrote: ----

Hi Foss
If that can help you

Sub UpDateTxtFile(
Dim FSO As Object, File As Object, Contents As Strin
Set FSO = CreateObject("Scripting.FileSystemObject"

' Create file to test example
Set File = FSO.CreateTextFile("c:\examplefile.txt"
Contents = "A quick example of the ReadAll method !!!
Contents = Contents & vbCrL
Contents = Contents & "New line for this example !!!
File.Write Content
File.Clos

' Replacement routin
Const TxtFullPath As String = "c:\examplefile.txt
Dim ReadFile As Objec
Set ReadFile = FSO.OpenTextFile(TxtFullPath, 1, False
Contents = ReadFile.ReadAl
ReadFile.Close: Set ReadFile = Nothin
With CreateObject("VBScript.RegExp"
.Global = Tru
.IgnoreCase = Fals
.Pattern = "[!" & vbCrLf & "]
Contents = .Replace(Contents, ""
End Wit
Set File = FSO.CreateTextFile(TxtFullPath
File.Write Content
File.Clos
Set File = Nothing: Set FSO = Nothin
End Su

Regards
M
 
Hi Foss,
in your procedure, test with:
.Pattern = "[ ]"
Contents = .Replace(Contents, Chr(10))

MP

Foss said:
Hi there MP,

Thanks, that's great! One more question though, how can I replace a space with a linefeed?

I've tried using the Chr(10) thing but it doesn't seem to work.

Cheers,
Foss


----- Michel Pierron wrote: -----

Hi Foss,
If that can help you:

Sub UpDateTxtFile()
Dim FSO As Object, File As Object, Contents As String
Set FSO = CreateObject("Scripting.FileSystemObject")

' Create file to test example:
Set File = FSO.CreateTextFile("c:\examplefile.txt")
Contents = "A quick example of the ReadAll method !!!"
Contents = Contents & vbCrLf
Contents = Contents & "New line for this example !!!"
File.Write Contents
File.Close

' Replacement routine
Const TxtFullPath As String = "c:\examplefile.txt"
Dim ReadFile As Object
Set ReadFile = FSO.OpenTextFile(TxtFullPath, 1, False)
Contents = ReadFile.ReadAll
ReadFile.Close: Set ReadFile = Nothing
With CreateObject("VBScript.RegExp")
.Global = True
.IgnoreCase = False
.Pattern = "[!" & vbCrLf & "]"
Contents = .Replace(Contents, "")
End With
Set File = FSO.CreateTextFile(TxtFullPath)
File.Write Contents
File.Close
Set File = Nothing: Set FSO = Nothing
End Sub

Regards,
MP

Foss said:
Mornin' all,
certain symbols with either nothing or a line return.
got some pointers or code that would help me?
Foss
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top