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

Using the VBScript Drives Collection in ASP

This article demonstrates how to access information about disk drives attached to a web server supporting ASP.

The techniques described here could have many uses, such as:

  • Making sure a server supporting uploading of files has sufficient disk space available before large files are uploaded.
  • Creating an ASP document that will allow remote monitoring of disk space while the server administrator is out of town.

Starting out

The version of VBScript supplied with Internet Information Server (IIS) version 4.0 and above is supplied with several objects that are able to interact with and extract information from the machine's filesystem. Although the FileSystemObject object is commonly used to read and write text files from ASP documents, it has other uses, as will be demonstrated here...

The following script demonstrates the VBScript Drives Collection:

<%
'Script to display a list of drives connected to this machine

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

Set Drives = FileSystemObject.Drives
'Create a drives collection

'step through the drive collection, and extract
'the drive letter and the type of drive
'according to the Microsoft VBScript documentation
'there are 6 distinct types of drive


For Each DiskDrive in Drives

DriveLetter = DiskDrive.DriveLetter
DriveType = DiskDrive.DriveType

Select Case DriveType
Case "0" DriveType = "
Unknown type of drive"
Case "1" DriveType = "
Removable drive"
Case "2" DriveType = "
Fixed drive"
Case "3" DriveType = "
Network drive"
Case "4" DriveType = "
CD-ROM drive"
Case "5" DriveType = "
RAM Disk"
End Select

Response.Write "
Drive " & DriveLetter & " is a " & DriveType & "<BR>"

Next

Set Drives = nothing
Set FileSystemObject = nothing
%>

If you run this script, it should report the status of the drives attached to your system, e.g.

ASP Script output

In this example, the A drive has been identified as a removable drive (it's the floppy disk), drive E is also removable (a ZIP disk), drive G is the CD-ROM, and the rest are fixed drives (i.e. hard disks) with the exception of drive J, which is mapped to a network drive.

Further drive properties

As can be seen from the code sample above, two properties of the Drives collection are accessed: DriveLetter and DriveType. There are a total of 12 properties that can be obtained about a specific drive:

  • AvailableSpace: space left on the drive (in bytes)
  • DriveLetter: the drive's letter
  • DriveType: the type of drive (e.g. CD-ROM, network drive etc.)
  • FileSystem: the type of file system used on the specificed drive (e.g. NTFS, FAT)
  • FreeSpace: space left on the drive available to the current user (in bytes)
  • IsReady: indicates whether the drive can be accessed
  • Path: the path to the specified drive
  • RootFolder: returns a folder object for the given drive
  • SerialNumber: the unique number for the specified drive
  • ShareName: the network share name for the drive (if it has one)
  • TotalSize: the total amount of storage space on the drive
  • VolumeName: the name of the drive

Many of the properties (e.g. AvailableSpace, FileSystem) can only be accessed by VBScript directly checking the drive in question. This will cause script errors if there is an attempt to check a drive that is not available (e.g. the floppy disk drive is empty).

Fortunately it is possible to check to see if a drive is ready by using the drive's IsReady property:

<%
'Script to display a list of drives connected to this machine and also to check if a disk drive is ready

Set FileSystemObject = Server.CreateObject("Scripting.FileSystemObject")
Set Drives = FileSystemObject.Drives

For Each DiskDrive in Drives

DriveLetter = DiskDrive.DriveLetter
DriveType = DiskDrive.DriveType

Select Case DriveType
Case "0" DriveType = "
Unknown type of drive"
Case "1" DriveType = "
Removable drive"
Case "2" DriveType = "
Fixed drive"
Case "3" DriveType = "
Network drive"
Case "4" DriveType = "
CD-ROM drive"
Case "5" DriveType = "
RAM Disk"
End Select

Response.Write "Drive " & DriveLetter & " is a " & DriveType & " "

'If the drive is ready, display a blue piece of text.
'If the drive is not ready, display a red piece of text.


If DiskDrive.IsReady then
Response.Write "
<FONT COLOR=#0000FF>This drive is ready for use</FONT>"
Else
Response.Write "
<FONT COLOR=#FF0000>This drive is not ready for use</FONT>"
End If

Response.Write "<BR>"

Next

Set Drives = nothing
Set FileSystemObject = nothing
%>

This script can be easily tested by putting in or removing a floppy disk or CD-ROM from the disk drive. This will toggle the display for the A drive, as shown below:

Drive A is not ready 
Drive A is ready for use

Script enhancements

A further enhancement of the scripts shown here would be to show a graphical representation of the various drive types. A future article shows how to build a graphical ASP drive space indicator.

Don't forget as well that this code could also be used as the basis of a Windows Scripting Host script to continuously monitor disk resource usage.

Finally, always ensure that you know exactly who is able to view such information about your system. The less information you reveal to the whole Internet community, the better.

  Site Map | Privacy Policy

All content is 1995 - 2012