Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am trying to obtain the size of a directory on disk as displayed in the Properties of a folder.

Using FindFirstFile, I can get the size of each file in the directory but this is less than the size I want. I assume what is missing is the size of the directory file itself (I am not using compression).

FindFirstFile gives me the attributes, etc. of the directory but the size (both high and low) is zero.

Similarly GetFileSize returns zero. MSDN gives GetFileSize as one of the functions available on a directory (http://msdn.microsoft.com/en-us/library/aa365258(VS.85).aspx).

In my simple test below, both iDLowSize and iDHighSize are returned as zero. If I change the name to being a non-directory (file), the size is returned OK. Apologies but the code is C.

hCreate = CreateFile("c:\\Windows", GENERIC_READ, 0, NULL, OPEN_EXISTING,
        FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (hCreate == INVALID_HANDLE_VALUE) {// Error  }
iDLowSize = GetFileSize(hCreate, &iDHighSize);
CloseHandle(hCreate);


Any help will be appreciated.
Posted
Updated 24-Aug-10 7:59am
v2

Hi,

The difference between the actual file size and the amount of space used on the disk is due to the way space for files is allocated in fixed size blocks called clusters which are typically 4096 bytes.

The disk space allocated to a file can be calculated by rounding up the actual file size to the next integral multiple of the cluster size.

The GetDiskFreeSpace API will retrieve the cluster size for a volume.

Examples can be found at Understanding and Using GetDiskFreeSpace and GetDiskFreeSpaceEx[^] and http://www.pinvoke.net/default.aspx/kernel32/GetDiskFreeSpace.html[^]

Alan.
 
Share this answer
 
v3
Thanks for the explanation. I will experiment with the cluster size.

To clarify I think you are saying that a directory itself doesnt occupy any space?? Or at least it isnt included in the 'size on disk' in the folder properties.
Why does MSDN say that GetFileSize is a function for use on a directory if it will always return zero??

Thanks again

John
 
Share this answer
 
Comments
Alan N 25-Aug-10 9:00am    
The directory information does occupy space although precisely where and how much is dependent on the disk format. On FAT systems space is allocated for each individual directory whereas NTFS uses one index called the Master File Table (MFT). This is preallocated and expanded when it becomes full. Some light reading on file system internals can be found at http://www.ntfs.com.

"Or at least it isnt included in the 'size on disk' in the folder properties" TRUE

As for MSDN, who can say? It may be the link to GetFileSize in the directory management reference is a mistake as that topic refers to file handles only. Clearly GetFileSize will accept a directory handle but why it does appears to be a secret!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900