Visual Basic Programming Code Examples Visual Basic > Files Directories Drives Code Examples Finding Free Disk Space Finding Free Disk Space Public Declare Function GetDiskFreeSpace Lib "kernel32" _ Alias "GetDiskFreeSpaceA" (ByVal lpRootPathName As String, _ lpSectorsPerCluster As Long, lpBytesPerSector As Long, _ lpNumberOfFreeClusters As Long, lpTotalNumberOfClusters As Long) _ As Long Function GetFreeSpaceBytes(DriveRoot As String) As Long 'Dimension some variables. Dim TNOC As Long 'Total Number of Clusters Dim NOFC As Long 'Number Of Free Clusters Dim SPerC As Long 'Sectors Per Cluster Dim BPS As Long 'Bytes Per Sector 'Work out and return. GetDiskFreeSpace DriveRoot, SPerC, BPS, NOFC, TNOC GetFreeSpaceBytes = ((TNOC * SPerC) - (NOFC * SPerC)) * BPS End Function Function GetFreeSpaceKilobytes(DriveRoot As String) As Long 'Dimension some variables. Dim TNOC As Long 'Total Number of Clusters Dim NOFC As Long 'Number Of Free Clusters Dim SPerC As Long 'Sectors Per Cluster Dim BPS As Long 'Bytes Per Sector 'Work out and return. GetDiskFreeSpace DriveRoot, SPerC, BPS, NOFC, TNOC GetFreeSpaceKilobytes = (((TNOC * SPerC) - (NOFC * SPerC)) * BPS) / 1024 End Function 'ALL THE FOLLOWING ROUND TO DECIMALS - CHANGE "AS SINGLE" 'TO "AS LONG" TO AVOID THIS PROBLEM. Function GetFreeSpaceMegabytes(DriveRoot As String) As Single 'Dimension some variables. Dim TNOC As Long 'Total Number of Clusters Dim NOFC As Long 'Number Of Free Clusters Dim SPerC As Long 'Sectors Per Cluster Dim BPS As Long 'Bytes Per Sector 'Work out and return. GetDiskFreeSpace DriveRoot, SPerC, BPS, NOFC, TNOC GetFreeSpaceMegabytes = ((((TNOC * SPerC) - (NOFC * SPerC)) * BPS) / 1024) / 1024 End Function Function GetFreeSpaceGigabytes(DriveRoot As String) As Single 'Dimension some variables. Dim TNOC As Long 'Total Number of Clusters Dim NOFC As Long 'Number Of Free Clusters Dim SPerC As Long 'Sectors Per Cluster Dim BPS As Long 'Bytes Per Sector 'Work out and return. GetDiskFreeSpace DriveRoot, SPerC, BPS, NOFC, TNOC GetFreeSpaceGigabytes = (((((TNOC * SPerC) - (NOFC * SPerC)) * BPS) / 1024) / 1024) / 1024 End Function