You'd like to be able to gather specific information about the disk drives in your computer: for example, how large they are, how much space is free, whether they're local or remote, and whether they're removable or not. Access does not provide this information. Is it available using a Windows API function?
The Windows API provides three functions that you can use to extract information about the drives in your computer: GetLogicalDriveStrings, which returns a string containing a list of all the logical drives; GetDriveType, which returns information about the specified drive; and GetDiskFreeSpace, which returns information about the total and free disk space for a specified drive.
Load and run frmDiskSpace from 11-12.MDB. This form, shown in Figure 11-14, contains a list box with information about all the logical drives in your system. To fill the list box, the example code walks through all the drives returned from a call to GetLogicalDriveStrings, calling the other two functions for each drive.
To use these functions in your own applications, follow these steps:
Import the modules basDiskInfo and basToken from 11-12.MDB.
To call the functions, use the information in Table 11-12. Each function takes only a single parameter, the drive to be interrogated.
Function |
Purpose |
Return value |
Example |
---|---|---|---|
acbGetFreeSpace |
Retrieve the amount of free space on the specified drive. |
Variant (the amount of free disk space, in bytes), or Null if the function failed |
lngFree = acbGetFreeSpace("C") |
acbGetTotalSpace |
Retrieve the total amount of space on the specified drive. |
Variant (the amount of total disk space, in bytes), or Null if the function failed |
lngTotal = acbGetTotalSpace("C") |
acbIsDriveCDROM |
Verify that a drive is a CD-ROM. |
True if CD-ROM, False otherwise |
fCD = acbIsDriveCDROM("D") |
acbIsDriveFixed |
Verify that a drive is a hard disk. |
True if a hard disk, False otherwise |
fFixed = acbIsDriveFixed("C") |
acbIsDriveLocal |
Verify that the specified drive is local. |
True if local, False if remote |
fLocal = acbIsDriveLocal("C") |
acbIsDriveRAMDisk |
Verify that a drive is a RAM disk. |
True if RAM disk, False otherwise |
fRAM = acbIsDriveRAMDisk("F") |
acbIsDriveRemote |
Verify that the specified drive is a network drive. |
True if remote, False if local |
fNetwork = acbIsDriveRemote("E") |
acbIsDriveRemovable |
Verify that the specified drive is for removable media. |
True if removable, False otherwise |
fRemovable = acbIsDriveRemovable("A") |
The sample form doesn't actually use any of the acbIs functions listed in Table 11-12; these functions are supplied only for your own applications. Instead, it calls the acbGetDrives function in basDiskInfo, which fills an array of acb_tagDriveInfo structures directly with information about each of the installed drives, physical or logical.
The structure looks like this:
Type acb_tagDriveInfo strDrive As String varFreeSpace As Variant varTotalSpace As Variant fRemovable As Boolean fFixed As Boolean fRemote As Boolean fCDROM As Boolean fRamDisk As Boolean End Type
It stores all the information that the sample form displays. The sample form then uses a list-filling callback function to display the information in a list box. (For more information on list-filling callback functions, see Chapter 7.)
The acbGetDrives function starts out by calling the Windows API function GetLogicalDriveStrings. This function returns a string containing all the logical drives on your machine, in this format:
C:0D:0G:0H:0
where the 0s indicate null characters, Chr$(0). (VBA provides the vbNullChar constant that's equivalent to Chr$(0).) The acbGetDrives function loops through this string, using the acbGetToken function in basTokens to pull out the drive names, one at a time, and then gathering information about each. The source code for acbGetDrives is:
Public Function acbGetDrives(astrDrives( ) As acb_tagDriveInfo, _ fIncludeFloppies As Boolean) ' Fill astrDrives( ) with all the available logical drive letters. Dim strBuffer As String Dim intCount As Integer Dim intI As Integer Dim varTemp As Variant Dim lngType As Long Const conMaxSpace = 1024 strBuffer = Space(conMaxSpace) intCount = GetLogicalDriveStrings(conMaxSpace - 1, strBuffer) strBuffer = Left(strBuffer, intCount) intI = 1 intCount = 0 Do varTemp = acbGetToken(strBuffer, vbNullChar, intI) If Len(varTemp & "") > 0 Then ' The next statement will be true except in the ' case where the drive < C and you DON'T want ' to include floppies. Then it'll skip the drive. If (UCase(Left(varTemp, 1) < "C")) Imp fIncludeFloppies Then intCount = intCount + 1 ' Get the drive name. astrDrives(intCount).strDrive = varTemp ' Get the drive type, and set the flags accordingly. lngType = GetDriveType(varTemp) Select Case lngType Case DRIVE_REMOVABLE astrDrives(intCount).fRemovable = True Case DRIVE_FIXED astrDrives(intCount).fFixed = True Case DRIVE_REMOTE astrDrives(intCount).fRemote = True Case DRIVE_CDROM astrDrives(intCount).fCDROM = True Case DRIVE_RAMDISK astrDrives(intCount).fRamDisk = True End Select ' Get the drive space information. astrDrives(intCount).varTotalSpace = acbGetTotalSpace(varTemp) astrDrives(intCount).varFreeSpace = acbGetFreeSpace(varTemp) End If intI = intI + 1 End If Loop Until Len(varTemp & "") = 0 acbGetDrives = intCount End Function
The acbGetTotalSpace and acbGetFreeSpace functions both call the private GetDiskSpace function, which in turn calls the GetDiskFreeSpace API function. GetDiskSpace takes the four pieces of information returned from GetDiskFreeSpace?sectors per cluster, bytes per sector, free clusters, and total clusters?and returns the calculated value that you've requested:
Private Function GetDiskSpace(ByVal strDrive As String, _ fTotal As Boolean) As Variant ' Input: ' strDrive: String representing drive letter ' fTotal: True for total space on drive, False for free space on drive ' Output: ' Free or Total space, if no error. Null, otherwise. Dim lngSectorsPerCluster As Long Dim lngBytesPerSector As Long Dim lngFreeClusters As Long Dim lngTotalClusters As Long ' Force the string into the correct format. strDrive = Left(strDrive, 1) & ":\" If GetDiskFreeSpace(strDrive, lngSectorsPerCluster, lngBytesPerSector, _ lngFreeClusters, lngTotalClusters) Then GetDiskSpace = lngSectorsPerCluster * lngBytesPerSector * IIf(fTotal, _ lngTotalClusters, lngFreeClusters) Else GetDiskSpace = Null End If End Function
If you want to dig a bit further, investigate the GetVolumeInformation API function. This function retrieves even more information about the specified drive, including its volume name, serial number, whether or not compression is enabled, the filesystem type (FAT, HPFS, NTFS), and other information about how data is stored on that drive. This information is of less importance to Access developers than to system application developers, so we don't discuss it here.