Overview
This article is first of a series of articles about working with drives, folders and
files using ASP. In this article we'll learn how to get hold of Drives collection
which FileSystemObject provides us, then we'll use this handle to display a
list of all drives connected to our computer with their detailed info. These drives may
be fixed hard disks, removable drives like floppy drives, CD-ROM drives or even network
drives. We'll then use the handle to a drive to access root folder for that drive and
then display all the sub folders and files in that drive. We'll build our application
in such a way that we can move around different drives and folders and read different
files.
To sum up you'll learn how to get hold of drives, display list
of folders and subfolders, get hold of a folder and display list of files it contains along
with their detailed info, and also how to read a file and display it's contents using ASP.
Why would I need to work with folders and files?
With any web application you build sooner or later you come across the need to read and
write files to the server hard disk. ASP makes it easy for you. ASP allows you to do
just about anything you can think of with files using FileSystemObject that comes
with IIS.
What are the security considerations?
Well any application on your web site which allows you to read and write files to
your server hard disk can be potential security threat if someone else gets access to that
ASP page. So as a rule of thumb you should always password protect such applications
and don't make them public.
What will I need?
Nothing, IIS comes preloaded with FileSystemObject. You'll have no
problem in using it on your own computer or your site server. Remember though that some
virtual ASP hosting account providers don't allow you to use FileSystemObject
on their servers, so before using it on your server always check and see if your ASP hosting
provider supports that. Almost all good ASP hosts I know off support FileSystemObject.
Following is a list of topics we'll cover in this article :
Working with Drives
We will build an ASP page drive.asp that will display a list of all
drives on your system. As explained on the previous page we'll be using FileSystemObject.
There is no need to manually create drive.asp page. At the end of this
article you can download all the source code.
Lets first create FileSystemObject.
<%
Dim fso
Set fso = Server.CreateObject("Scripting.FileSystemObject")
There is property of FileSystemObject called Drives which
returns a collection of drive objects available on the local machine. We get hold of that
property by using the ASP Set keyword. Note that whenever you want to reference an
object or collection always use Set keyword.
Dim drives
Set drives = fso.Drives
Now drives variable points to the Drives Collection. Notice
that Drives Collection is not a single Drive object, it
rather is a Collection of Drive objects.
To reference each element from a collection we use standard ASP For...Each
loop.
Dim isReady
For Each drive in drives
isReady = drive.IsReady
If isReady Then
Response.Write "DriveLetter: " & drive.DriveLetter & "<br>"
Response.Write "Path: " & drive.Path & "<br>"
Response.Write "FileSytem: " & drive.FileSystem & "<br>"
Response.Write "TotalSize: " & drive.TotalSize & "<br>"
Response.Write "FreeSpace: " & drive.FreeSpace & "<br>"
Else
Response.Write "Driv Letter: " & drive.DriveLetter & "<br>"
Response.Write drive.Path & "<br>"
Response.Write "No other information available."
End If
Next
In the code above we first declare a variable isReady and then enter the
For..Each loop. This loop iterates through all the elements of Drives
collection. Within the loop we get hold of individual Drive object and use
Response.Write to display Drive object's different properties.
You'll be wondering what IsReady property of Drive object means.
Well this property returns a boolean value ( True or False ) indicating if the
drive is ready or not. What do we mean by a drive being ready or not ? A drive is ready when
it is connected to the system and working e.g. our fixed hard disks. A drive is not ready e.g.
when there is no CD in CD-ROM drive or there is no floppy in the floppy drive.
When we are done with FileSystemObject and Drive objects we
explicitly remove them from server memory using the Nothing keyword.
Set drives = Nothing
Set fso = Nothing
%>
Note that drive.asp page present in the download contains the same code plus
some extra code to display the same results using more convenient to read HTML coding.
Working with Folders
Here we will build folder.asp page to display a list of all subfolders
and files within a given folder. Like before there is no need to copy/paste the code, simply
try to understand what is going on, all the source code is available for download on the
last page.
We begin by creating the FileSystemObject.
<%
Dim fso
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Lets get reference to the root folder for our website using FileSystemObject's
GetFolder method.
Dim rootFolder
Set rootFolder = fso.GetFolder(Server.MapPath("/"))
Now we use the handle to the root folder of our web site to get a collection of sub folders
using Folder object's SubFolders property.
Dim subFolders
Set subFolders = rootFolder.SubFolders
We are now ready to use For..Each loop to display a list of all sub folders
under the root folder.
For Each folder in subFolders
Response.Write "FolderName: " & folder.Name & "<br>"
Response.Write "Attributes: " & folder.Attributes & "<br>"
Response.Write "Type: " & folder.Type & "<br>"
Response.Write "Size: " & folder.Size & "<br>"
Response.Write "ShortName: " & folder.ShortName & "<br>"
Next