0

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?

Cyber Avater
  • 1,494
  • 2
  • 9
  • 25

1 Answers1

2

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.

gidds
  • 16,558
  • 2
  • 19
  • 26
  • Can You link me to one for Linux + Kotlin (or Java) at least? – Cyber Avater Jun 25 '23 at 17:18
  • See the linked question. Any Java library is available to Kotlin/JVM. – gidds Jun 25 '23 at 17:20
  • So, du will give me `Size on Disk` on linux? – Cyber Avater Jun 25 '23 at 17:29
  • [du](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/du.html) is a command-line program; it's part of the POSIX spec., so Linux, macOS, and other Unix-like OSs should have it. You can run it (as with all command-line programs) using the Java [ProcessBuilder ](https://docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html) class, though it's not particularly easy or efficient. – gidds Jun 25 '23 at 17:37
  • I want to ask you an off-topic question, Do you know any way to get directory size (any Size or Size on Disc) very fast in Android? My walk().sumOf is very slow. I've seen MiXplorer List directories very fast with their sizes, but unfortunately, it's closed-sourced. – Cyber Avater Jun 25 '23 at 17:43
  • I don't know Android, sorry. That would probably be better as a new question. – gidds Jun 25 '23 at 17:52