Home |
Search |
Today's Posts |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I'd 'dump' the entire file into an array using VB's standard file I/O
functions (rather than FSO) to read into a string, and VB's Split function to put the lines into an array specifying 'vbCrLf' as the delimiter. Use VB's 'Filter' function to clear the array of empty elements (extra 'vbCrLf' lines) so the array ends up with only lines of data. 'Dump' the array back into a string using VB's 'Join' function and specifying 'vbCrLf' as the delimiter. Then 'dump' the string back into the file. Some reusable functions... Read from file: Function ReadTextFileContents(Filename As String) As String ' A reuseable procedure to read large amounts of data from a text file in one single step. Dim iNum As Integer On Error GoTo ErrHandler iNum = FreeFile(): Open Filename For Input As #iNum ReadTextFileContents = Space$(LOF(iNum)) ReadTextFileContents = Input(LOF(iNum), iNum) ErrHandler: Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description End Function 'ReadTextFileContents() Write to file: Sub WriteTextFileContents(Text As String, Filename As String, Optional AppendMode As Boolean = False) ' A reuseable procedure to write, overwrite, or append large amounts of data ' to a text file in one single step. Dim iNum As Integer On Error GoTo ErrHandler iNum = FreeFile() If AppendMode Then Open Filename For Append As #iNum: Print #iNum, vbCrLf & Text; Else Open Filename For Output As #iNum: Print #iNum, Text; End If ErrHandler: Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description End Sub 'WriteTextFileContents() ***Note that the 'Print' statement lines end with a semi-colon to avoid extra lines at the end of the file. This write procedure does not add extra lines and so when appending to the file a vbCrLf needs to be put in front of the text.*** Filtering out the empty lines of an existing file: Dim vText As Variant, sTextIn As String sTextIn = ReadTextFileContents(<full_filename) vText = Split(sTextIn, vbCrLf) vText = Filter(vText, "", False) Write back to the file: WriteTextFileContents(Join(vText, vbCrLf), <full_filename) Append to file: WriteTextFileContents(Join(vText, vbCrLf), <full_filename, True) -- Garry Free usenet access at http://www.eternal-september.org ClassicVB Users Regroup! comp.lang.basic.visual.misc |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
FileSystemObject help please. | Excel Programming | |||
FileSystemObject help please. | Excel Programming | |||
Filesystemobject Help | Excel Programming | |||
filesystemobject | Excel Programming | |||
FileSystemObject | Excel Programming |