ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Editing text file (https://www.excelbanter.com/excel-programming/300275-editing-text-file.html)

Foss

Editing text file
 
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

Michel Pierron[_2_]

Editing text file
 
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" a écrit dans le message de
...
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




Foss

Editing text file
 
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

"Foss" a écrit dans le message d
..
Mornin' all
I need to open a text file, then replace all occurences o

certain symbols with either nothing or a line return
I really don't know where to start on this one, has anyon

got some pointers or code that would help me
Thanks very much

Fos





Michel Pierron[_2_]

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

MP

"Foss" a écrit dans le message de
...
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" a écrit dans le message de
...
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








All times are GMT +1. The time now is 03:27 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com