It is sometimes useful to determine the amount of free space on a disk, or even the total space on a disk drive. The Windows API provides the functions GetDiskFreeSpace and GetDiskFreeSpaceEx for this purpose, but they are often cumbersome to use. In fact, the GetDiskFreeSpace routine is not available on systems older than Win/95B(OSR2), and it is tricky to write a single function that would work on all 32-bit versions of Windows.
The function GET_DISK_SPACE gets the relevant information in one easy call, and the values are returned in floating point variables in units of MegaBytes.
The routine is available as part of the C++ source file EXAM50.CPP. The routine has the following calling parameters:
The four arguments are interpreted as follows:
The root directory name must be supplied, while the three space
values are returned by the routine.
In most cases, the "available space" is the same as the "free space".
However, in Win/2000 (and higher), if disk quotas are enabled, then
the available space denotes the space available for the current user,
and it may be less than the free space on disk.
"Megabytes" are computed in units of 1024*1024 bytes. Thus if the
routine returns a space of 1.00 MB, it really means 1,048,576 bytes.
You can download the file in ZIP format
by clicking on the following link.
This works for Microsoft Windows 3.x(Win32s)/95/98/ME/NT/2000.
I have tested with Visual C++ v6.0 but it should work with other versions
also.
Please let me know of exceptions.
After downloading, unzip the file Exam50.cpp into any folder.
It contains a short console mode program which demonstrates how to
call GET_DISK_SPACE.
Note: The routine GET_DISK_SPACE calls the routine GET_WIN_VERSION, and
the source for the latter is also included.
If you have any questions please send them to the e-mail address below.
Disclaimer:
Return to Ravi's Free Software Page
int GET_DISK_SPACE(const char *,float *,float *,float *)
where the drive name must be specified in the form of it's
root directory name (e.g. c:\ or d:\) and all other values are
returned by the routine.
The return value is 0 for success, and non-zero for an error condition.
1st : Root Directory of Disk Drive (e.g. c:\ or d:\)
2nd : Available Space in Megabytes
3rd : Total Space in Megabytes
4th : Free Space in Megabytes
This site contains some software that I've written over the years.
It is believed to work, but there is no warranty.
Please use at your own risk.
If you notice any bugs please let me know by email at
kochhar@physiology.wisc.edu.
Return to Ravi's Home Page
Back to The Basement
This page last modified on : July 27, 2003