Free Disk Space

Special thanks to Shuan C for the following code and explanations.

--------------------------------- Start Here---------------------------------

Option Explicit
'This is the API Function to get the various parameters
'of the disk.

Private 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
Private Sub Form_Click()
'The following variables are passed ByRef to the API
'which fills it with the appropriate values, which we
'can use in the subsequent code.

Dim sectors_per_cluster As Long, bytes_per_sector As Long, _
number_of_free_clusters As Long, Total_Number_of_Clusters As Long, _
ReturnValue As Long
Dim Root As String
'This denotes the Drive for which we want to determine the free space.
'In a commercial application, we'll probably obtain it from a Drive list box
'Here I've hard coded it for simplicity's sake

Root = "c:\"
ReturnValue = GetDiskFreeSpace(Root, sectors_per_cluster, bytes_per_sector, _
number_of_free_clusters, Total_Number_of_Clusters)
'The following expression gives the freespace in Bytes
Dim FreeSpaceInBytes As Long
FreeSpaceInBytes = number_of_free_clusters * sectors_per_cluster * bytes_per_sector
'The following expression gives the freespace in MB
Dim FreeSpaceMB As Long
FreeSpaceMB = FreeSpaceInBytes / 1048576
'You can divide it by appropriate constants to display the freespace in KB, GB etc.
'Additionally, you may want to determine the units depending on
'the amount of freespace that is available.
'After all, it doesn't make much sense to tell the user that
'"Your floppy disk has 0.000345GB free" does it? :)
'The total space on the drive can be determined as follows

Dim TotalDiskSpaceinBytes As Long
TotalDiskSpaceinBytes = Total_Number_of_Clusters * sectors_per_cluster * bytes_per_sector
'Again you can use all the previous numerical manipulations to display
'it in KB, MB, GB etc. Also you can subtract the freespace from this to determine the used space, etc etc
'But I'm sure you don't need me to tell you all about that.
'Now we display some of the results

MsgBox "The disk " & Root & "has " & FreeSpaceInBytes & " bytes free" & _
"out of a total " & TotalDiskSpaceinBytes & " bytes"
End Sub

--------------------------------- End Here---------------------------------

Note: The above code does NOT perform any error handling. So don't use it in a production environment, as it is.

 

Hope I've made the function clear. If your target environment is NT4 or higher or W95 OSR2 or higher, there is a better alternative.

The GetDriveSpaceEx, which as the name suggests has some extra features. An example is given below.

#################################################################################

#                                                Function Description                                                             #

#  The GetDiskFreeSpaceEx function obtains information about the amount of space available      #

#  on a disk volume: the total amount of space, the total amount of free space, and                      #

#  the total amount of free space available to the user associated with the calling thread.              #

#################################################################################

Create a new project, and add this code to Form1:

--------------------------------- Start Here---------------------------------


Private Declare Function GetDiskFreeSpaceEx Lib "kernel32" Alias "GetDiskFreeSpaceExA" (ByVal lpRootPathName As String, lpFreeBytesAvailableToCaller As Currency, lpTotalNumberOfBytes As Currency, lpTotalNumberOfFreeBytes As Currency) As Long
Private Sub Form_Click()
    Dim ReturnValue As Long, BytesFreeToCalller As Currency, TotalBytes As Currency
    Dim TotalFreeBytes As Currency, TotalBytesUsed As Currency
    'the drive to find
    Const RootPathName = "C:\"
    'get the drive's disk parameters
    Call GetDiskFreeSpaceEx(RootPathName, BytesFreeToCalller, TotalBytes, TotalFreeBytes)
    'show the results, multiplying the returned value by 10000 to adjust for the 4 decimal
    'places that the currency data type returns.

    MsgBox " Total Number Of Bytes:", Format$(TotalBytes * 10000, "###,###,###,##0") & " bytes"
    MsgBox "Total Free Bytes:", Format$(TotalFreeBytes * 10000, "###,###,###,##0") & " bytes"
    MsgBox "Free Bytes Available:", Format$(BytesFreeToCalller * 10000, "###,###,###,##0") & " bytes"
    MsgBox "Total Space Used :", Format$((TotalBytes - TotalFreeBytes) * 10000, "###,###,###,##0") & " bytes"
End Sub

--------------------------------- End Here---------------------------------

Copyright ©2000 by Shuan C.  Please read our disclaimer.