Search code examples
windowswinapidirectorysizedelphi-7

delphi - calculate directory size API?


Anyone knows other way to get a directoy's size, than calculate it's size by counting file with file? I'm interested on some win32 API function. I've google it about this, but i didn't found relevant information so i'm asking here.

PS: I know how to calculate a directory size by using findfirst and findnext and sum all file's size.


Solution

  • Getting the size of one directory is a pretty big problem, mostly because it's something you can't define. Examples of problems:

    • Some filesystems, including NTFS and most filesystems on Linux support the notion of "hard links". That is, the very same file may show up in multiple places. Soft links (reparse points) pose the same problem.
    • Windows allows mounting of file systems to directories. Example: "C:\DriveD" might be the same thing as "D:\".
    • Do you want the file size on disk or the actual size of the file?
    • Do you care about actual DIRECTORY entries? They also take up space!
    • What do you do with files you don't have access to? Or directories you don't have permission to browse? Do you count those?

    Taking all this into account means the only possible implementation is a recursive implementation. You can write your own or hope you find a ready-written one, but the end performance would be the same.