Link to home
Start Free TrialLog in
Avatar of SimonORorke
SimonORorke

asked on

Space on hard disc drive used by file system

A small amount of space on each hard disc drive is reserved for use by the file system.  The size of this can be shown by running the DOS command CHKDSK.  For FAT drives, CHKDSK shows it as "... bytes in ... directories".  For NTFS drives, CHKDSK shows it as "... kilobytes in ... indexes", "... kilobytes in use by the system." and "... kilobytes occupied by the logfile."  I would like to know which Win32 API function I can use to get the same information.
Avatar of mikeblas
mikeblas

You need to walk the file system yourself if you need this information, as the operating doesn't expose documented APIs that provide it. (It's odd information to need, and in the case of WinNT, it's not very safe to worry about.)

.B ekiM


Avatar of SimonORorke

ASKER

I have already looked at the Win32 Programmer's reference and did not spot an API function that looked like it could do it.  So, yes, I did suspect that any API function or combination of functions that would do the trick might be undocumented.  But some people seem to know about undocumented API functions...  I agree that it is odd information to need.  But I have a (totally innocent) reason!
if you are using Delphi you can use DiskFree and if you are using Borland C you can use getdfree, at this time iam not sure if MSCompiler can suport this function...
note: this is not an API but have support to win16 and win32...

#include <dos.h>
void getdfree(unsigned char drive, struct dfree *dtable);

struct dfree {
   unsigned df_avail;     /* available clusters */
   unsigned df_total;     /* total clusters */
   unsigned df_bsec;      /* bytes per sector */
   unsigned df_sclus;     /* sectors per cluster */
};

i hope this will help you,
lortega
That information doesn't answer the question, lortega. It doesn't include the amount of space reserved by the operating system, or used by on-disk data structures.

.B ekiM

mikeblas is correct:  GetDiskFreeSpace does not give the information I want.
Use GetDiskFreeSpace API Fuction.

ex)

GetDiskFreeSpace("c:\\",&a,&b,&c,&d);

FreeSpce = a*b*c;
TotalSpace = a*b*d;

ok ?
Uh, didn't you read the previous comments, jhjeon?

..B ekiM

From the experience that I have had with a similar problem, you have to recursively add up the space used....it doesn't actually take too long, but it would be nice if there was a simple one line function call.. :-)
Can you explain how to find the pieces of space to add up?  If I have to scan the whole drive, it may take too long for my purpose.  Biut I would like to find out more.  This is for a new version of Folder Sizer, my freeware disc space analysis program.  The program calculates the physical size of each folder tree.  But it never adds up exactly to the drive size minus the free space.  One reason is the space used by the system.  I was hoping to show this as well, for completeness.  But if I cannot do it, or it takes too long, I shall do without it!
Surely if you know the amount of space that is used for all the folders, and you know the free space and the total disc size, you can effectively say the remainder is for the system?
Part of the remaining space might be bad sectors.

Like I said a couple of weeks ago, you have to crawl the disk structures yourself if you want this info. And, yes: it can take a very long time!

..B ekiM

As mikeblas says, part of the remaining space might be bad sectors.  Other possible parts I have thought of are: (1) lost clusters, which, as opposed to bad ones, can be recovered for use, for example by SCANDISK;  (2) unavoidable inaccuracies in the way I calculate physical file sizes (the inaccuracy, if any, would always be in the direction of underestimation).  This will all be extensively explained in the help file of Folder Sizer 2.0.

Now I would like to explore mikeblas's comment that "you have to crawl the disk structures yourself if you want this info".  Well, in a way, my program already does.  I have to navigate to the end of every branch of the tree of folders in order to include the size of each file in the appropriate tree sizes and folder sizes.  Now, if there was something extra I could do while navigating the folder tree to also pick up the system space (and possibly even the bad and lost clusters), it might not add much to the overall time taken.  However, I suspect that what mikeblas terms "crawling the disc structures" is a completely different procedure compared to what I am already doing.  Is that correct?

If so, it would presumably at least double the time taken. CHKDSK is the quickest way I know to discover the amount of system space.  If a comparison with that is anything to go by, the time taken would much more than double.  Unless I can pick up the system space using the navigation I am already doing, it will not be worth attemting.  

If you can clarify this, mikeblas, please pose your comment as an answer.  I would consider my question answered as far as I need it.  I would probably rate you're your answer "satisfactory".  This feels like a strange thing to have to decide:  I reckon I should only give a rating of "excellent" for an actual way to get the system space size;  but maybe that is unfair if I have concluded that it is not feasible in the context of what I am doing.  I dunno!
The GetDiskFreeSpaceEx function could give u  info about the amount of space available on a disk volume: the total amount of space, the total amount of free space

Didn't you read any of the previous responses, lekshmikr?  GetDiskFreeSpaceEx() won't provide the information SimonORorke is asking for.

..B ekiM
By "crawling the disk structures", I mean that you'll need to do the low-level analysis yourself. That is, you'll have to read the FAT and directory structures and add things up yourself.  Or, you'll need to get Helen Custer's NTFS book and do the same thing for the NTFS structures.

Adding up directory totals will make it hard to get the right answer; there's rounding, hidden space, reserved space, the NTFS index pages, and all sorts of stuff.

Maybe your best alternative, if you really must do this, is to run spawn CHKDSK and capture it's output. Of course, that leaves you in the lurch if CHKDSK finds any errors.

..B ekiM

As already explained in previous comments, GetDiskFreeSpace does not provide the infomation I need.
I think I have heard enough now.  Thanks for the explanations mikeblas.  I had considered spawning CHKDSK and trapping its result.  But it sounds too risky and would be too slow anyway.  And it is clear that any other method would make my program run for much too slowly for what it is worth to me.  Unless anyone else has any brilliant suggestions (i.e. not GetDiskFreeSpace!), I shall withdraw the question in a couple of days.
I didn't intend to offer my answers, insight, and advice for free.

..B ekiM

I am puzzled by mikeblas's latest comment.  As I said in a previous comment, I am quite willing to give you points, mikeblas.  But, as I said, you will have to pose your comments as an answer so that I can give you the points.  On the other hand, I have not used Experts Exchange much:  so may be there is a trick to it that I have not understood.  If so, please let me know what i need to do.  Otherwise, send an answe,r as I suggested.
ASKER CERTIFIED SOLUTION
Avatar of mikeblas
mikeblas

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial