Which is better: files or textStreams?

R

Rhino

I have a macro that reads one external file and that writes entries to a log
file, if and when it encounters errors. Both the external file and the log
file are simple text files.

I know from the VBA help that I can work with either files or textStreams
but I don't see anything in the help that says why one would be better than
the other. Can anyone enlighten me on this subject?

What are the pros and cons of files vs. textStreams?

At the moment, I have my options open, but I'd like to make a decision about
which to use. For instance, my macro currently does the following:

Private Const WRITEONLY As Integer = 2
Private Const LOG_IS_TEXTFILE = True
Private Const LOG_FILE_NAME As String = "c:\word_log.txt"

If (LOG_IS_TEXTFILE) Then
Set logFileObject = CreateObject("Scripting.FileSystemObject")
logFileObject.CreateTextFile (LOG_FILE_NAME)
Set logFile = logFileObject.GetFile(LOG_FILE_NAME)
Set logTextStream = logFile.OpenAsTextStream(WRITEONLY,
TRISTATE_USE_DEFAULT)
Else
logFileNumber = FreeFile
Open LOG_FILE_NAME For Output As logFileNumber
End If

If (LOG_IS_TEXTFILE) Then
logTextStream.write "Unexpected key, " & resumeFileKey & ", found in
file." & vbCrLf
Else
Write #logFileNumber, "Unexpected key, " & resumeFileKey & ", found
in file."
End If

If (LOG_IS_TEXTFILE) Then
logTextStream.Close
Else
Close logFileNumber
End If


But this is silly: surely one or the other is the better choice so I'd like
some advice on which to use so that I can remove the code that writes the
other format.

By the way, one related question. When I have LOG_IS_TEXTFILE set to False,
every line written into the log file has a pair of quotes around it but this
doesn't happen when the boolean is set to True. Why is that?
 
J

Jezebel

If you trawl the VB forum, you'll see that the consensus is that the
FileSystemObject is something that you wish hadn't stood in. There is really
nothing it does that you can't do just as easily using native commands -- so
why add the complexity of another library?
 
R

Rhino

Thanks for settling that!

I was leaning towards files anyway - fewer lines of code to do the same
thing, particularly on the open - but I thought there might be some big
benefit to textStreams that I didn't know about, like performance or
security. Since there apparently isn't any such benefit, I'll stick to good
old files then.

--
Rhino


Jezebel said:
If you trawl the VB forum, you'll see that the consensus is that the
FileSystemObject is something that you wish hadn't stood in. There is
really nothing it does that you can't do just as easily using native
commands -- so why add the complexity of another library?
 

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

Top