About   -   Contact   -   Purchase   -   Search   -   What's New

Get Free Disk Space
This function returns number of Mbytes of free disk space when passed the drive letter to be interrogated. Add the following code to a FileHandling.bas standard module to compile a File Utility function set:
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
Function GetFreeDiskSpace(Drive As String) As Single
'<DESC>		Interrogates available disk free space in
'		Mbytes on specified drive</DESC>
'<RETURN>	Free drive capacity (Mb)
'			</RETURN>
'<ACCESS>	Public
'<ARGS>		Drive to Interrogate
'				</ARGS>
'<USAGE>	sngDriveSpace = GetFreeDiskSpace("c:\")
'				</USAGE>
    Dim lngSectors_per_cluster As Long
    Dim lngBytes_per_sector As Long
    Dim lngFree_clusters As Long
    Dim lngTotal_clusters As Long
    If GetDiskFreeSpace(Drive, lngSectors_per_cluster, _
      lngBytes_per_sector, lngFree_clusters, _
      lngTotal_clusters) = 0 Then
        GetFreeDiskSpace = -1
        GetFreeDiskSpace = Format$(((lngFree_clusters / 1000000) * _
          lngSectors_per_cluster * lngBytes_per_sector), _
    End If

End Function

Top of Page

Legal Notice

Ken Howe 2011