Previous Section Table of Contents Next Section

Working with Drives

Drive objects represent the logical drives attached to your system, including network drives, CD-ROM drives, and so forth. Drives also provide an entry point into each drive's file system, starting with the root folder of the file system hierarchy. Because the Drive object represents one of the simplest aspects of the file system, it's one of the simplest objects in the FSO.

The method you'll use most with drives is GetDrive, which returns a Drive object given a specific drive letter. For example, to obtain a Drive object that represents your C: drive:


Dim oDriveC, oFSO

Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")

Set oDriveC = oFSO.GetDrive("C:")

You can also use the FSO's root-level Drives collection to iterate through all of the drives attached to your system.


Dim oFSO, oDrive

Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")

For Each oDrive In oFSO.Drives

 MsgBox "Drive " & oDrive.DriveLetter & _

  " has a capacity of " & oDrive.TotalSize & " bytes " & _

  " and is drive type " & oDrive.DriveType

Next

Working with Drive Objects

The previous example illustrates the use of some of the Drive object's properties. The full list includes the following.

  • AvailableSpace and FreeSpace return the number of bytes available on the disk. FreeSpace returns the amount of free space on the drive; AvailableSpace returns the amount available to the user running the script. File quotas and other concerns can result in a difference between these two properties.

  • DriveLetter returns the drive's logical letter. Note that not all drives must have a drive letter, especially in Windows 2000 or later, although most of the time they will.

  • DriveType tells you what kind of drive you're looking at. This property returns a number corresponding to a specific drive type.

  • FileSystem tells you what kind of file system the drive uses. This is a string, such as FAT, NTFS, or CDFS (used for optical media like CDs and DVDs).

  • IsReady returns a True or False. This is mainly useful for network and removable drives, and allows you to see if they're ready (connected or with a disk inserted) before trying to use them.

  • Path returns the drive letter and the root folder; for example, "C:\".

  • RootFolder returns a Folder object representing the root folder of the file system.

  • SerialNumber returns the drive's volume serial number.

  • ShareName gives you the share name (UNC) for network drives, such as "\\Server1\Share." For non-network drives, this property returns an empty string.

  • TotalSize is the total size of the drive in bytes. To figure the size in kilobytes, divide by a thousand; to find megabytes, divide by one million; for gigabytes, divide by a billion.

  • VolumeName gives you the name of the drive's logical volume.

NOTE

The term drive can be confusing. In Windows, and therefore in the FSO, a drive is a logical entity. More than one drive can live on a disk, with the disk being the physical device. The terms drive and volume are more or less interchangeable as far as the FSO is concerned.


When working with the DriveType property, the following values correspond to specific drive types.

  • 0: Unknown. This is rare, although some weird devices like tape backup drives hooked into a parallel port can show up this way.

  • 1: Removable. This applies to any removable media drive, such as a floppy or Zip disk, but not to optical media drives.

  • 2: Fixed. This is used for all hard drives, and for some devices that aren't recognized as removable, like older FireWire drives.

  • 3: Network. This is used for all mapped network drives.

  • 4: CD-ROM. This is used for all optical media drives, including DVD-ROMs.

  • 5: RAM Disk. This is rare, as most of us don't use RAM disks anymore. Note that USB "pen" drives show up as either type 1 or 2, not as RAM disks.

The base FSO object has a couple of other interesting methods for working with drives, including DriveExists, which accepts a drive letter and returns a True or False indicating whether the drive exists. This is useful for checking to see if a drive exists before trying to work with it. Note that GetDrive returns an error if the drive you specify doesn't exist, so using DriveExists first is always a good idea.

Listing 12.1 shows an example of how the FSO's Drive object can be used to iterate through available drives and set the volume name for all fixed drives to "Fixed."

Listing 12.1. NameDrives.vbs. Changes the volume name for fixed drives to "Fixed."

Dim oFSO, oDrive

Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")

For Each oDrive In oFSO.Drives

 If oDrive.Type = 2 Then

  If oDrive.VolumeName <> "Fixed" Then

   oDrive.VolumeName = "Fixed"

  End If

 End If

Next

MsgBox "Finished!"

This script illustrates an important concept, which is that some Drive properties are writable and others aren't. For example, you can change the VolumeName property, which changes the actual name of a drive. However, you cannot change the TotalSize property. Although it might be nice to have a script expand the size of your drives, it just isn't possible!

Another important concept is the RootFolder property. Unlike the other properties, which return a value of some kind, RootFolder returns a completely new Folder object, which represents the root folder of the drive.

    Previous Section Table of Contents Next Section