Search code examples
kotlinfilesystemsfilesize

Calculate 'Size on Disk' of a directory


I can get the recursive size of a directory by:

    val sz = Path("""D:\Games""").walk().sumOf { it.fileSize() }

On Windows, In the properties of a directory, there are Size and Size on Disk. Now, how do I get the Size on Disk?


Solution

  • I doubt there's a way to do this, unfortunately.

    I think it would need a system call, as the result will depend upon the exact details of the filesystem, including things such as block size, deduplication/data sharing, block packing, logging, compression, etc. (Some filesystems are very complex!)

    So this isn't something you can do in pure Kotlin, and would need a suitable library with implementation(s) for the platform(s) you're interested in. But I'm not aware of any such library, and the answer to this question (which asks for Java way) suggests that there isn't one.