Declare Function GetDiskFreeSpace Lib "kernel32.dll" Alias "GetDiskFreeSpaceA" (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, lpTotalNumberOfClusters As Long) As Long
GetDiskFreeSpace gives information on the free space on a disk, the total space, and more! It puts four pieces of information into each of the numeric variables passed to it. A "cluster" is the actual unit of storage on a disk. A cluster can hold no more than 1 file, but a file can be spread across multiple clusters. A "sector" is a part of a cluster. You can calculate the cluster size in bytes by lpSectorsPerCluster * lpBytesPerSector. You can calculate the disk's free space by multiplying the cluster size by lpNumberOfFreeClusters, and the disk's total space by multiplying the cluster size by lpTotalNumberOfClusters. The function returns 0 if an error occured, or a non-zero value if it succeeded.
Example:
' Find the free space on the hard drive C:
x = GetDiskFreeSpace("c:\", SecPerClus, BytePerSec, FreeClus, TotalClus)
Debug.Print "Free space:"; SecPerClus * BytePerSec * FreeClus; "bytes"
Category: Files
Back to the index.