Helpful Information
 
 
Category: Software Design
Retrieve a Folder -or- Directory Size

I have tried in vain to retrieve the size of a Windows NTFS folder through a VC++ Application.

If any one could please help a novice C++ programmer I would greatly appreciate it.

Thanks in advance...

Beans4You

I remember helping you traverse an NTFS directory a while earlier.
http://forums.devshed.com/showthread.php?s=&threadid=39656

All you need to do is add some more code to sum up the file sizes. You can get the filesize of each file from the WIN32_FIND_DATA structure. For more info on the structure, see this:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wceobjst/htm/cerefWIN32_FIND_DATA.asp

So the filesize would be computed in your code as something like this:
(data.nFileSizeHigh * MAXDWORD) + data.nFileSizeLow


Add them up into a global variable and you should be set. BTW I think this post really belongs in the C/C++ forum rather than the algorithms forum.

Scorpions4ever,

First, thanks for all your help - now and past -

Second, why use the (data.nFileSizeHigh * MAXDWORD)? I always get a zero as a value when calling data.nFileSizeHigh.

Third, I have done something similar but not using the (data.nFileSizeHigh * MAXDWORD). My problem is, is that the algorithm will not enter directories such as "System Volume Information" and therefore I get a total which is smaller than the value returned by Windows in the properties of the directory, and a strange phenomena also happens. I can run the function several times right after each other and get slightly different results each time.

What I was hoping to find is some attribute or system call which I could send the directory path and get the size. There has to be some way to do it, I mean how does Windows perform this task? Every time you request the properties of a directory does it go through the trouble to recursively search the directory?

Eric

>> First, thanks for all your help - now and past -

I'm always glad to help :)

>> Second, why use the (data.nFileSizeHigh * MAXDWORD)? I always get a zero as a value when calling data.nFileSizeHigh.

Because the documentation from Micro$oft says to do so. If you check out the link that I posted above (the one to MSDN), you'll see that it says:
"nFileSizeHigh
High-order DWORD value of the file size, in bytes. This value is zero unless the file size is greater than MAXDWORD. The size of the file is equal to (nFileSizeHigh * MAXDWORD) + nFileSizeLow."

Normally, nFileSizeHigh will be 0, unless your filesize exceeds MAXDWORD, which is 2^32-1 (around 4+GB). There are few files currently available that are this large, but in the future, this may become more common. Can anyone say "bloatware!" :D


>>Third, I have done something similar but not using the (data.nFileSizeHigh * MAXDWORD). My problem is, is that the algorithm will not enter directories such as "System Volume Information" and therefore I get a total which is smaller than the value returned by Windows in the properties of the directory, and a strange phenomena also happens. I can run the function several times right after each other and get slightly different results each time.

Without seeing your code, I can't really tell you what's wrong. BTW if you check the properties of the System Volume Information folder in Explorer, the size returned is 0 bytes, so I suspect this isn't affecting your total. Maybe the size of your total is being affected by cached files or cookies, if you're websurfing at the same time?


>>What I was hoping to find is some attribute or system call which I could send the directory path and get the size. There has to be some way to do it, I mean how does Windows perform this task? Every time you request the properties of a directory does it go through the trouble to recursively search the directory?

I'm not aware of any such call. I think Explorer also recursively searches to compute the size of the directory, because I can actually see it adding up the file sizes, when I check the properties of a large folder (such as Program Files). After it computes it for the first time, I think it caches the value so that the next time around, the computation is much faster. There is a fast way to get available space or used space for a drive though, instead of going through the directories. For this purpose, you can use GetDiskFreeSpace() or GetDiskFreeSpaceEx().

Hope this helps!

A most in-depth answer.

Thank you much.

:)










privacy (GDPR)