Transact SQL Other Articles Software Reviews
An ASP Disk Space Monitor
A previous article describes how to use the Drives collection to access information about disk drives attached to an ASP web server. This article expands on the ideas presented in that article by describing how to create a dynamic disk space monitor. The techniques used here could be used to ensure that there is enough disk space before a user uploads a large file to the server, or perhaps the code could be used to help create a web based server monitoring tool so that the server administrator can keep a track of server resources when miles from home.
The typical output from this script is shown in the image below. For each accessible drive connected to the system, the drive letter is printed, together with the drives volume name (if it has one). The amount of used space on a disk is shown in red, free space shown in blue. This effect is created by resizing red and blue GIF images to give a representation of the total amount of disk space on a given drive. Additionally, the images ALT tags have been set up so that they give the amount of disk space (in the example below, the ALT tag for the C: drives free space shows that there is 227.1Mb of free disk space).
If you want the images used above, they are available in a ZIP file which is linked from the bottom of this article.
Dissecting the Script
The first line of code creates a FileSystemObject object. This is a powerful object when used with ASP its most common use is for reading and writing text files on the server, but here the Drives property is used to find out information about the drives connected to the server:
Set FileSystemObject = Server.CreateObject("Scripting.FileSystemObject")
The following line of code establishes the Drives object as a Drives collection:
Set Drives = FileSystemObject.Drives
The Drives collection will contain information about all of the drives connected to the server, so a For Each Next loop is used to loop through each member of the Drives collection:
For Each DiskDrive in Drives
One thing to remember is that although a drive is connected to a server, it may not be
accessible. The script will generate errors if it attempts to access certain drive
properties if that drive is not ready. Removable media such as a floppy, CD-ROM or ZIP
drives will not be accessible unless there are disks in the drives. Fortunately, the
IsReady property of each drive can be used to check that the drive is ready:
From the TotalSize and FreeSpace drive properties, it is possible to work out the amount of disk space in use on that drive:
totalspace = DiskDrive.TotalSize
Disk space is reported as bytes, so the following code will convert the space to Mb. It will also convert free and used space as a percentage of the total space on a drive:
freepercent = Int((freespace/totalspace)*100)
The following code is used to format the text that will be used for the ALT tags that display disk space. To make the display look nicer, it shows space less than 1 Mb in size as Kb, and space greater than 1024 Mb as Gb. The FormatNumber function is used to specify the number of decimal places that the numbers should be rounded to:
If freespace =< 1048576 Then
There is also a bit of a hack for drives that have no free or used space this is reported as 0 bytes:
If Left(freelabel,3) = "0.0" Then freelabel = "0 bytes"
The ALT tags for the free and used space are then prepared:
used_alt_tag = "Drive " & DiskDrive.DriveLetter & ": " & usedlabel
& " in use"
The drive letter and (if it has one) volume name are then output using Response.Write statements:
Response.Write "Drive " & DiskDrive.DriveLetter & ": "
If DiskDrive.VolumeName <> "" then
Further Response.Write statements are then used to display the graphical representation of disk space. Note that the width of the free and used space indicators are altered according to the percentage calculated earlier. The actual image is composed of four elements: a left border, the used space indicator, the free space indicator, and finally the right border.
The next two lines of code will allow the script to proceed to the next drive in the Drives collection:
The final lines of code release the created objects from system memory:
Set Drives = nothing
And thats it. Hopefully you should be able to find a use for the script dont hesitate to email me if you find an interesting use for it.
For convenience, the entire code for the script is given below.
The entire code for the disk monitoring script
Images used by the Disk Monitoring Script
If you would like the four images used with this script (created using Kai's Power Tools 5), then download this ZIP file.