FileSystemObject? Use the
If you've ever needed to move, delete, copy, or create text files in the past you've likely used the
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
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
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/.)
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
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
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
to Import DLL Constants!
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
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
Charset properties. For our first example,
we'll be working with textual information in ASCII format.
Now that we've indicated the
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
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
are cleared, the contents of the file are loaded into the stream, and the position is set to zero (the beginning
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
To do this we'll simply use the
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
(For more information on the
IUSR_machinename account, be sure to read:
What is the
Once we are done with the
Stream object, be sure to
Close it and set it to Nothing.
As I mentioned before, the
Stream object can also handle binary files. To work with binary files,
Type property to
adTypeBinary and use the
methods as opposed to
WriteText. A quick example of reading a GIF file
and outputting it can be seen in the code below:
[Run the binary
To learn more about the
ContentType property of the Response object, be sure to read:
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
object... but I'll save that for a later article!
StreamObject Technical Information