Simple Drivespace Question

What can I do to get the current available space on a specific drive?  What are some pitfalls and tricks I should be aware of?
LVL 1
BigOneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
You didn't mention the OS (thus, a comment). For Win32, the functions of choice are 'GetDiskFreeSpace()' and 'GetDiskFreeSpaceEx()'.

Pitfalls: None, except that NTFS can handle files >4GB and that 'GetDiskFreeSpaceEx()' isn't available on the first release of Win95...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nietodCommented:
standard C++ provides no facilities for obtaining this.  Many OS's however, have OS-specific functions that you can call to obtain this information, for exmple in windows you can use the GetDiskFreeSpace() function.

What OS are you using?
0
nietodCommented:
Opps, a little late.
0
chensuCommented:
Windows 95:
The GetDiskFreeSpace function returns incorrect values for volumes that are larger than 2 gigabytes. The function caps the values stored into *lpNumberOfFreeClusters and *lpTotalNumberOfClusters so as to never report volume sizes that are greater than 2 gigabytes.
Even on volumes that are smaller than 2 gigabytes, the values stored into *lpSectorsPerCluster, *lpNumberOfFreeClusters, and *lpTotalNumberOfClusters values may be incorrect. That is because the operating system manipulates the values so that computations with them yield the correct volume size.

Windows 95 OSR2 and Windows 98:
The GetDiskFreeSpaceEx function is available on beginning with Windows 95 OEM Service Release 2 (OSR2). The GetDiskFreeSpaceEx function returns correct values for all volumes, including those that are greater than 2 gigabytes.

To determine whether GetDiskFreeSpaceEx is available, call GetModuleHandle to get the handle to Kernel32.dll. Then you can call GetProcAddress.

The following code fragment shows one way to do this:

pGetDiskFreeSpaceEx = GetProcAddress( GetModuleHandle("kernel32.dll"),
                         "GetDiskFreeSpaceExA");

if (pGetDiskFreeSpaceEx)
{
   fResult = pGetDiskFreeSpaceEx (pszDrive,
                (PULARGE_INTEGER)&i64FreeBytesToCaller,
                (PULARGE_INTEGER)&i64TotalBytes,
                (PULARGE_INTEGER)&i64FreeBytes);

// Process GetDiskFreeSpaceEx results.
}

else
{
   fResult = GetDiskFreeSpace (pszDrive,
                &dwSectPerClust,
                &dwBytesPerSect,
                &dwFreeClusters,
                &dwTotalClusters)

// Process GetDiskFreeSpace results.

}

It is not necessary to call LoadLibrary on Kernel32.dll because it is already loaded into every Win32 process's address space.


On Windows NT 4.0 or later, use GetDiskFreeSpaceEx.
0
BigOneAuthor Commented:
Thanks for your help, sorry about the OS, it is NT4.0, my bad.  Good answer.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.