Brettb.Com
  HOME | ABOUT ME | BIOTECHNOLOGY | ARTICLES | GALLERY | CONTACT
Search: Go
TECHNICAL ARTICLES
 ASP
 ASP.NET
 JavaScript
 Transact SQL
 Other Articles
 Software Reviews

PHOTO GALLERIES
 Canon EOS 300D Samples
 Akihabara Maids!
 More Galleries...

TRAVEL LOG
 2009: China
 2008: Tokyo
 2007: Tokyo
 2006: Hong Kong
 2005: New York City

MORE STUFF
 Search Engine Optimisation
 Build an ASP Search Engine
 My Tropical Fishtank
 Autoglass
 SQL Month Name
 SQL Get Date Today
 SQL Year Month
 Other New Stuff...

POPULAR STUFF
 Regular Expressions
 Index Server & ASP
 JavaScript Ad Rotator

Home > Articles

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 drive’s 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: drive’s free space shows that there is 227.1Mb of free disk space).

Example output from the ASP disk space monitoring script

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:

If DiskDrive.IsReady Then

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
freespace = DiskDrive.FreeSpace
usedspace = totalspace - freespace

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)
freemb = Int((freespace/1024)/1024)
usedmb = Int((usedspace/1024)/1024)
usedpercent = Int((usedspace/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
freelabel = FormatNumber(freespace/1024, 1) & "
Kb"
Elseif freespace =< 1073741824 then
freelabel = FormatNumber((freespace/1024)/1024, 1) & "
Mb"
Else
freelabel = FormatNumber(((freespace/1024)/1024)/1024, 2) & "
Gb"
End If

If usedspace =< 1048576 Then
usedlabel = FormatNumber(usedspace/1024, 1) & "
Kb"
Elseif usedspace =< 1073741824 Then
usedlabel = FormatNumber((usedspace/1024)/1024, 1) & "
Mb"
Else
usedlabel = FormatNumber(((usedspac e/1024)/1024)/1024, 2) & "
Gb"
End If

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"
If Left(usedlabel,3) = "0.0" Then usedlabel = "
0 bytes"

The ALT tags for the free and used space are then prepared:

used_alt_tag = "Drive " & DiskDrive.DriveLetter & ": " & usedlabel & " in use"
free_alt_tag = "
Drive " & DiskDrive.DriveLetter & ": " & freelabel & " free"

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
Response.Write "
[" & DiskDrive.VolumeName & "] : "
End If

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.

Response.Write "<img align=absmiddle src=bb-drives-diskindicator-leftborder.gif>"
Response.Write "
<img align=absmiddle src=bb-drives-diskindicator-used.gif width=" & usedpercent & " height=29 alt=" & Chr(34) & used_alt_tag & Chr(34) & ">"
Response.Write "
<img align=absmiddle src=bb-drives -diskindicator-free.gif width=" & (100-usedpercent) & " height=29 alt=" & Chr(34) & free_alt_tag & Chr(34) & ">"
Response.Write "
<img align=absmiddle src=bb-drives-diskindicator-rightborder.gif>"

Response.Write "
<br>"

The next two lines of code will allow the script to proceed to the next drive in the Drives collection:

End If
Next

The final lines of code release the created objects from system memory:

Set Drives = nothing
Set FileSystemObject = nothing

And that’s it. Hopefully you should be able to find a use for the script don’t 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

<%
'An ASP disk space monitoring script
'See http://www.brettb.com/ASPDiskMonitoringScript.asp


'Create a FileSystemObject object
Set FileSystemObject = Server.CreateObject("Scripting.FileSystemObject")

Set Drives = FileSystemObject.Drives

'Step through the drives collection, and extract information about any drive that is ready
For Each DiskDrive in Drives

If DiskDrive.IsReady Then

'The following lines work out the amount of used and free space on a disk. ASP reports disk space in bytes.
'Divide bytes by 1024 to get Kb, Kb by 1024 to get Mb, and so on...


totalspace = DiskDrive.TotalSize
freespace = DiskDrive.FreeSpace
usedspace = totalspace - freespace

freepercent = Int((freespace/totalspace)*100)
freemb = Int((freespace/1024)/1024)
usedmb = Int((usedspace/1024)/1024)
usedpercent = Int((usedspace/totalspace)*100)

'If the amount of disk space is below one Mb then report freespace as Kb.
'A similar thing is done for Mb and Gb of space.


If freespace =< 1048576 Then
freelabel = FormatNumber(freespace/1024, 1) & "
Kb"
Elseif freespace =< 1073741824 then
freelabel = FormatNumber((freespace/1024)/1024, 1) & "
Mb"
Else
freelabel = FormatNumber(((freespace/1024)/1024)/1024, 2) & "
Gb"
End If

If usedspace =< 1048576 Then
usedlabel = FormatNumber(usedspace/1024, 1) & "
Kb"
Elseif usedspace =< 1073741824 Then
usedlabel = FormatNumber((usedspace/1024)/1024, 1) & "
Mb"
Else
usedlabel = FormatNumber(((usedspace/1024)/1024)/1024, 2) & "
Gb"
End If

If Left(freelabel,3) = "0.0" Then freelabel = "
0 bytes"
If Left(usedlabel,3) = "0.0" Then usedlabel = "
0 bytes"

used_alt_tag = "
Drive " & DiskDrive.DriveLetter & ": " & usedlabel & " in use"
free_alt_tag = "
Drive " & DiskDrive.DriveLetter & ": " & freelabel & " free"

Response.Write "
Drive " & DiskDrive.DriveLetter & ": "

If DiskDrive.VolumeName <> "" then
Response.Write "
[" & DiskDrive.VolumeName & "] : "
End If

'Create the graphical representation of the freespace
Response.Write "<img align=absmiddle src=bb-drives-diskindicator-leftborder.gif>"
Response.Write "
<img align=absmiddle src=bb-drives-diskindicator-used.gif width=" & usedpercent & " height=29 alt=" & Chr(34) & used_alt_tag & Chr(34) & ">"
Response.Write "
<img align=absmiddle src=bb-drives-diskindicator-free.gif width=" & (100-usedpercent) & " height=29 alt=" & Chr(34) & free_alt_tag & Chr(34) & ">"
Response.Write "
<img align=absmiddle src=bb-drives-diskindicator-rightborder.gif>"

Response.Write "
<br>"
End If
Next

Set Drives = nothing
Set FileSystemObject = nothing
%>

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.

Further Reading

  Site Map | Privacy Policy

All content is 1995 - 2012