Class: APIDisks in Category Windows : File and Disk Operations from Total Visual SourceBook

Class for working with disk drives using the Windows API for VB6 and 32 and 64-bit versions of VBA.

Although Visual Basic has rudimentary file and disk functionality, you must go to the Windows API to get real power in this area. This class exposes logical drives through a collection of properties and methods, including support for:

  • Enumerating all logical drives.
  • Getting drive information such as volume label, free and total space, serial number and more.
  • Determining if a drive is ready.
  • Changing a volume label.
Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the CAPIDisks class.
DiskFreeSpace Property Get the free space on the specified drive.
DiskTotalSpace Property Get the total disk space on the specified drive.
DriveFileSystemType Property Get the file system type for the specified logical drive. Possible return values for this function are: FAT, FAT32, NTFS, NTFSC.
DriveFlags Property Get the file system flags for the specified logical drive. This function returns a long integer that is the result of the call to the Windows API GetVolumeInformation function. To see a readable form of this long integer, pass it to the class VolumeFlagsToString method.
DriveLabel Property Get the label for the specified logical drive.
DriveSerialNumber Property Get the serial number for the specified logical drive.
DriveType Property Get the long integer value for the specified logical drive. For a readable translation of this value, call the class DriveTypeToString method instead.
LogicalDrives Property Get the drive letter of the specified logical drive.
DriveLetterFromNumber Method Get the drive letter for the specified logical drive number.
DriveTypeToString Method Translates long integer drive type value to a readable string. Use the DriveType property in this class to get the long integer value of the drive type, then use this method to get a readable form of the drive type.
GetDriveCount Method Get the number of logical drives installed.
IsDriveReady Method Determine if the named drive is ready for use. The Windows API does not have a native method for determining if a drive is ready.
VolumeFlagsToString Method Translates long integer file system flags value to a readable string. Use the DriveFlags property of this class to get the long integer file system flags. Then pass the value to this method to get the readable form of the flags.
WriteVolumeLabel Method Set the volume label for the specified disk. Use this method with caution. It physically changes the volume label for the specified disk.
GetDriveInfo Private Get the requested information about the specified drive.
TrimNull Private Get the passed string terminated at the first null character. If there isn't a null, the entire string is returned.
' Example of CAPIDisks
'
' To use this example, create a new module and paste this code into it.
' Then run the procedure by putting the cursor in the procedure and pressing:
'    F5 to run it, or
'    F8 to step through it line-by-line (see the Debug menu for more options)

Private Sub Example_CAPIDisks()
  ' Comments: Examples of using the CAPIDisks class to determine information on your disk drives using VBA or VB6.

  Dim clsDiskTest As CAPIDisks
  Dim intCounter As Integer
  Dim strText As String
  Dim intDriveCount As Integer
  Dim lngFlags As Long
  Dim lngType As Long
  Dim fIsReady As Boolean

  ' Initialize the class
  Set clsDiskTest = New CAPIDisks

  ' Display information about each drive
  intDriveCount = clsDiskTest.GetDriveCount
  Debug.Print "There are " & intDriveCount & " logical drives on this system: "

  ' Change the volume label for Drive C on logical drive 2
  ' Uncomment to code to see it work (Warning: this will change the volume label!)
  ' clsDiskTest.WriteVolumeLabel 1, "Foobar"

  ' Loop through each installed drive on the system. For each drive, build up the strText variable with information.
  For intCounter = 0 To intDriveCount - 1
    strText = "Logical drive " & intCounter & " " & vbCrLf & _
              "===========================" & vbCrLf

    ' Determine if the drive is ready
    fIsReady = clsDiskTest.IsDriveReady(intCounter)

    If Not fIsReady Then
      strText = strText & "Drive " & intCounter & " " & vbCrLf
    Else
      ' Get additional information
      strText = strText & "Letter      : " & clsDiskTest.LogicalDrives(intCounter) & vbCrLf
      strText = strText & "SerNo       : " & clsDiskTest.DriveSerialNumber(intCounter) & vbCrLf
      strText = strText & "Label       : " & clsDiskTest.DriveLabel(intCounter) & vbCrLf
      strText = strText & "FileSysType : " & clsDiskTest.DriveFileSystemType(intCounter) & vbCrLf

      lngFlags = clsDiskTest.DriveFlags(intCounter)
      strText = strText & "Flags       : " & lngFlags & " (" & clsDiskTest.VolumeFlagsToString(lngFlags) & ")" & vbCrLf

      lngType = clsDiskTest.DriveType(intCounter)
      strText = strText & "Type        : " & lngType & " (" & clsDiskTest.DriveTypeToString(lngType) & ")" & vbCrLf
      strText = strText & "Free Space  : " & FormatNumber(clsDiskTest.DiskFreeSpace(intCounter), 0) & vbCrLf
      strText = strText & "Total Space : " & FormatNumber(clsDiskTest.DiskTotalSpace(intCounter), 0) & vbCrLf
    End If

    ' Display what we know about the drive
    Debug.Print strText

  Next intCounter

  ' All done, so close the class
  Set clsDiskTest = Nothing

End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS