Brettb.Com
  HOME | ABOUT ME | BIOTECHNOLOGY | ARTICLES | TOOLS | GALLERY | CONTACT
Search: Go
DEVELOPER TOOLS
 ASP Documentation Tool
 .NET Documentation Tool
 PHP Documentation Tool
 SQL Documentation Tool
 VB6 Documentation Tool
 Indexing Service Companion
 The Website Utility

TECHNICAL ARTICLES
 ASP
 ASP.NET
 JavaScript
 Transact SQL
 Software Reviews

PHOTO GALLERIES
 Canon EOS 300D Samples
 Red Arrows 2004
 Living Coasts
 Akihabara Maids!
 Web Page Backgrounds
 More Galleries...

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

NEW STUFF
 ASP Spell Check
 Code Documentors
 The Website Utility
 Search Engine Optimisation
 Build an ASP Search Engine
 My Tropical Fishtank
 Text WorkBench
 Other New Stuff...

POPULAR STUFF
 Regular Expressions
 ASP Documentation Tool
 Index Server & ASP
 JavaScript Ad Rotator

LINKS
 Business Website
 ASPAlliance Articles
 Software Documentation Portal

Microsoft Certified Professional

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

Useful Development Tools

ASP Documentation Tool™
Automatically creates developer documentation for ASP 2.0 and 3.0 web applications written in VBScript and JScript. Documentation for Microsoft Access, SQL Server 7/2000 databases and Visual Basic 6.0 components associated with the web application can also be incorporated into the reports. Documentation is created in HTML, HTML Help and plain text formats.
   View Sample Output (HTML Help format) View Sample Output (HTML Help format).
   View Sample Output (HTML Format) View Sample Output (HTML Format).
   Download Trial Version Download Trial Version (5.2Mb ZIP file).

.NET Documentation Tool
Automatically creates technical documentation for .NET Framework Windows and ASP.NET applications written in C# or VB.NET and SQL Server 7/2000/2005 or Microsoft Access databases associated with the application. Documentation is created in HTML, HTML Help and plain text formats.
   View Sample Output (HTML Help format) View Sample Output (HTML Help format).
   View Sample Output (HTML Format) View Sample Output (HTML Format).
   Download Trial Version Download Trial Version (5Mb ZIP file).

SQL Documentation Tool
The SQL Documentation Tool creates technical documentation for Microsoft SQL Server 7.0 and 2000 databases. Technical documentation is created in HTML and HTML Help formats. The HTML Help format documentation is fully searchable and cross referenced. The SQL Documentation Tool documents SQL Server Tables, Views, Stored Procedures, Triggers and Table Relationships.
   View Sample Output (HTML Help format) View Sample Output (HTML Help format).
   View Sample Output (HTML Format) View Sample Output (HTML Format).
   Download Trial Version Download Trial Version (10.3Mb ZIP file).

VB Documentation Tool
The VB Documentation Tool creates technical documentation for Microsoft Visual Basic 6.0 projects. Technical documentation is created in HTML and HTML Help formats. The HTML Help format documentation is fully searchable and cross referenced.
   View Sample Output (HTML Help format) View Sample Output (HTML Help format).
   View Sample Output (HTML Format) View Sample Output (HTML Format).
   Download Trial Version Download Trial Version (1Mb ZIP file).

Indexing Service Companion
The Indexing Service Companion is a Windows application that extends the functionality of the Microsoft Windows Indexing Service so that it is able to index content from remote websites and also from ODBC databases. As such it can be used as a low cost alternative to Sharepoint Portal Search Services.
   Try Sample Search Facility Try Sample Search Facility.
   Download Trial Version Download Trial Version (1.7Mb ZIP file).

The Website Utility
The Website Utility examines websites for errors and areas that need to be optimised for search engines by using a built in web crawling engine. Errors checked for include broken or moved hyperlinks, missing page titles and missing meta tags. It also generates HTML for use in creating website site maps (table of contents pages - like this one), and is able to create both client-side JavaScript Search Engines and server-side ASP Search Engines for a website.
   View Sample Output (HTML Format) View Sample Output (HTML Format).
   Download Trial Version Download Trial Version (3Mb ZIP file).

PHP Documentation Tool™
Automatically creates developer documentation for PHP web applications. Documentation is created in HTML, HTML Help and plain text formats.
   View Sample Output (HTML Help format) View Sample Output (HTML Help format).
   View Sample Output (HTML Format) View Sample Output (HTML Format).
   Download Trial Version Download Trial Version (1.0Mb ZIP file).
ASP Documentation Tool - Free Trial Available!

Documentation tools to automate the documentation of SQL Server databases and ASP, C#, VB.NET and VB 6.0 application source code

  Site Map

All content is © 1995 - 2008 Brett Burridge