If you've ever needed to move, delete, copy, or create text files in the past you've likely used the
FileSystemObject. The FileSystemObject
can be used to provide file-specific information (such as date created, size, last modified date, etc.), and
also can allow for creating, moving, deleting, and renaming text files. However, the FileSystemObject
cannot work with binary files (without some painful and expensive character set conversions).

However, there is a way to work with both binary and text files without using the FileSystemObject!
This new method involves the use of the Stream object, available with ADO 2.5. (You can download
the latest version of ADO at: http://www.microsoft.com/data/.)

The Stream object contains a number of methods for reading and writing binary and text files.
(A live demo is available, but take a moment to first look at the code
presented in this article. Also, if this article piques your interest, be sure to check out the
technical docs on the Stream
object!)
Before reading or writing a stream, the first thing we need to do is to Open the stream:

Note that you must have ADO 2.5 or greater installed for the above code to work. If you do not have ADO 2.5
installed you will get an Invalid Class String error on the Server.CreateObject line
above. To learn what version of ADO you have, be sure to read this post!
Also note the METADATA tag at the top; this imports the ADO 2.5 DLL constants into our ASP page.
To learn more about METADATA, be sure to read: Using METADATA
to Import DLL Constants!

Calling the Open method without any parameters creates a zero length string. At any time you can
view the size of your string by referencing the Size property. Now that we've got a Stream,
we need to specify what type of information data we wish to deal with: binary or text (and if text, ASCII or Unicode).
This information can be supplied in the Type and Charset properties. For our first example,
we'll be working with textual information in ASCII format.

objStream.Type = adTypeText
objStream.Charset = "ascii"

Now that we've indicated the Type and Charset, we're ready to either read or write
information to our stream! We can either write information into it manually or through reading a file. Why
not do both!? We'll start by writing a text file to the stream;
to do this we can use the LoadFromFile method.

objStream.LoadFromFile "D:\Inetpub\wwwroot\webtech\083100-1.shtml"

Whenever you are working with a Stream, it is important that you know the current position in
the stream. When you use the LoadFromFile method, the entire contents of the Stream
are cleared, the contents of the file are loaded into the stream, and the position is set to zero (the beginning
of the Stream). If you were to write some string to the Stream at this point, it would start overwriting
the beginning of the Stream, since the position is currently 0. So, before we write anymore
data to the Stream, let's advance the position of the Stream to the end of the Stream.
To do this we'll simply use the Position property.

'Move the position to the end of the stream...
objStream.Position = objStream.Size

Now that we're sitting at the end of the stream, let's add in some hard-coded text (why not a blatant and
shameless plug? <evil grin>):

Now that we have our Stream full of information, let's write it to a file on our Web server.
Note that this file will be written as the IUSR_machinename account, so that account name
must have Write permissions on the directory where you wish to write the Stream. If adequate
permissions do not exist, you will receive an error when trying to use the SaveToFile method.
(For more information on the IUSR_machinename account, be sure to read:
What is the FileSystemObject Object?)

Once we are done with the Stream object, be sure to Close it and set it to Nothing.

'Close the stream and set it to nothing...
objStream.Close
Set objStream = Nothing
%>

As I mentioned before, the Stream object can also handle binary files. To work with binary files,
set the Type property to adTypeBinary and use the Read and Write
methods as opposed to ReadText and WriteText. A quick example of reading a GIF file
and outputting it can be seen in the code below:

To learn more about the ContentType property of the Response object, be sure to read:
Protecting Everything.

Well, there you have it, a way to work with both text and binary files without using the
FileSystemObject. There are a number of other neat, neat things you can do with the Stream
object... but I'll save that for a later article!

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.