LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default FileSystemObject

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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
FileSystemObject help please. Tom Ogilvy Excel Programming 0 July 8th 05 04:44 PM
FileSystemObject help please. Bob Phillips[_7_] Excel Programming 0 July 8th 05 04:44 PM
Filesystemobject Help CyndyG Excel Programming 4 May 17th 05 01:00 AM
filesystemobject Alvin Hansen[_2_] Excel Programming 3 January 27th 05 06:17 PM
FileSystemObject lol[_2_] Excel Programming 2 April 6th 04 09:56 PM


All times are GMT +1. The time now is 11:24 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"