Ensuring That a Drive is Ready

Microsoft® Windows® 2000 Scripting Guide

The script in Listing 4.3 has a potential flaw in it: If there is no floppy disk in the floppy disk drive that is being checked or no CD in the CD-ROM drive, the script will fail with a "Drive not ready" error. Drives that are not ready create problems for scripts that use the FileSystemObject; although the FileSystemObject can identify the existence of those drives, your script will fail if it attempts to access disk drive properties such as AvailableSpace or FreeSpace.

If a drive is not ready (which typically means that a disk has not been inserted into a drive that uses removable disks), you can retrieve only the following four drive properties:

  • DriveLetter

  • DriveType

  • IsReady

  • ShareName

Any attempt to retrieve the properties of another drive will trigger an error. Fortunately, the IsReady property allows the script to check whether a drive is ready before attempting to retrieve any of the properties that can trigger an error. The IsReady property returns a Boolean value; if the value is True, the drive is ready, and you can retrieve all the properties of the drive. If the value is False, the drive is not ready, and you can return only DriveLetter, DriveType, IsReady, and ShareName.

The script in Listing 4.4 returns a collection of disk drives installed on a computer. For each drive, the script uses the IsReady property to ensure that the drive is ready. If it is, the script echoes the drive letter and the amount of free space. If the drive is not ready, the script echoes only the drive letter, one of the four properties that can be accessed even if a drive is not ready.

Listing 4.4 Verifying That a Drive is Ready


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFSO.Drives
For Each objDrive in colDrives
 If objDrive.IsReady = True Then
 Wscript.Echo "Drive letter: " & objDrive.DriveLetter
 Wscript.Echo "Free space: " & objDrive.FreeSpace
 Wscript.Echo "Drive letter: " & objDrive.DriveLetter
 End If

note Note

  • This problem does not occur with WMI. If there is no disk in drive A or no CD in the CD-ROM drive, the script does not fail. Instead, WMI simply reports the amount of free space as Null.