![]() |
Table of Contents |
![]() |
Working with DrivesDrive 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 ObjectsThe previous example illustrates the use of some of the Drive object's properties. The full list includes the following.
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.
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. |
![]() |
Table of Contents |
![]() |