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:
|(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
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!
Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!
"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