Alvechurch Data - Drives in the File System Object

The File System Object (FSO) is part of the Windows Scripting Host and models the drives, folders, and files on your PC as a hierarchy of objects. The File System Object exposes useful properties and methods of these objects and is easier to use than the equivalent calls to the Windows API.

The FileSystem Obect resides in SCRRUN.DLL which has been supplied with a variety of Microsoft products and is present on most PCs.

How do I use it?

 Dim fso As FileSystemObject
 Set fso = New FileSystemObject

The Drives collection

All the drives on the PC are represented in the Drives collection and you can identify whether they are fixed, removable, or network drives. Removable disks appear in the collection whether or not the disk is in place.

Drives is a collection and has the usual Count and Item properties. It does not have an "Add" method - you can't use VB to create a new hard disk.

You can step through all the drives with code such as:

 Dim fso As FileSystemObject
 Dim drv As Drive

 Set fso = New FileSystemObject

 Debug.Print fso.Drives.Count

 For Each drv In fso.Drives
  Debug.Print drv.DriveLetter, drv.IsReady
 Next drv

Although the FSO can refer to floppy, zip, and CD drives regardless of whether there is a disk in that drive, you must check that IsReady is True before trying to read the FreeSpace or SerialNumber.

The Drive object

The Drive object has a range of properties. The most useful are:

Using the drive object:

Public Sub main()
 Dim fso As FileSystemObject
 Dim drv As Drive

  Set fso = New FileSystemObject

  Set drv = fso.GetDrive("a:")

  With drv
   If .DriveType = 1 Then
    If .IsReady = True Then
     If .FreeSpace > 737280 Then
      MsgBox "Plenty of space"
     Else
      MsgBox "Not enough space on the disk"
     End If
    Else
     MsgBox "Please put a disk in drive a:"
    End If
   End If
  End With
 End Sub

Problems

Several versions of the Windows Scripting Host exist but there seems to be no incompatibility between the File System Object components. A more serious problem is that some system administrators remove SCRRUN.DLL because it represents such a security risk. Any program or macro can call the Windows Scripting Host and wreak havoc.

We do not ship any programs that rely on its presence but we do use it for a lot of in-house projects and disk maintenance utilities.

Top of page
VB Tips