Link to home
Start Free TrialLog in
Avatar of gcorbett
gcorbett

asked on

MFC File / Directory Size Class

I have been looking for some time, but have been unable to find an MFC class to do directory / file sizing.

Is there such as thing ?

Thanks

Glenn Corbett
ASKER CERTIFIED SOLUTION
Avatar of rpb
rpb

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
Avatar of gcorbett
gcorbett

ASKER

Thanks,

What I was looking for is possibly a class (MFC), or library (.LIB) to do the work. I have a directory list calculator I got from one of the Microsoft samples (can't remember where). The problem with this is it requires a fair bit of work to turn it into a class. It basically does a recursive "walk" through the directories calculating the disk space as it goes.

What I am trying to do is have the user of the application select a user, list of users, or a server (which I have done), and then it will calculate the total amount of disk space being used in those directories. The code I have also seems to generate incorrect sizing, it is always above the actual allocation, and in the case of a large directory structure it can be several megabytes larger than Explorer or  DIR /S seems to think it is.

I assume with the CFile:: class that I would have to wrap it in Win32 to do the directory walking part. Is that correct ??

Glenn Corbett
The CFile class is not 32-bit specific - you can use it in 16-bit code as well.

Remember that the size needed to store a file is larger than the actual number of bytes in a file.  Forgive me if you know this, but it sounds from your comment that you are looking at the total file size in Explorer, which gives the total of the number of bytes in the selected files, whereas the amount of space required on disk is larger than this.  This is because of disk "cluster sizes".  A cluster is the minimum amount of disk space that can be allocated, and all allocations are in multiple of this size.  If you have, say, an 8k cluster size on your disk, then files of size 1 byte, 10 bytes, 100 bytes, 1000 bytes or 8192 (8k) bytes will all take up 8k of disk space.  It could be that your routine is working with allocation sizes, rather than file size, which is either more useful or less, depending on what you are trying to show.

If you wanted to do the directory walking, you may want to use the Win32 functions, as I am not sure CFile supports such functions.  Alternatively you could derive a class from CFile with the added functionality (say a CWalkingFile or something), and add cover functions for the three Win32 functions mentioned above.  The class could then keep track of its own progress in the walking using a member variable, which would keep things nice and encapsulated.
Thanks,

I eventually found the Microsoft PDC WIn32 sample which does   the directory walking, and encapsulated it into a C++ class. I removed the code which looks inside the list of found files, and it seems to work pretty well.

Glenn Corbett