Parag Bafna
Parag Bafna

Reputation: 22930

Directory Size in Objective C

Since NSFileManager has no capabilities of returning the size of a directory, what would be a suitable & fast method for obtaining directory size?

Upvotes: 1

Views: 1038

Answers (2)

mouviciel
mouviciel

Reputation: 67889

There is no fast method: you have to recursively request size of every file in the directory hierarchy.

Don't forget to handle special cases:

  • files (or directories on a Time Machine volume) with multiple hard links: count them only once.
  • special files such as what is found in /dev but can be created everywhere.
  • symbolic links: don't follow them, but count their size.

Depending on your needs, you also have to choose between size of file contents or size of file on disk.

Upvotes: 4

Girish Kolari
Girish Kolari

Reputation: 2515

Enumerate all directories inside your folder. "Folder size will be = Sum of All file sizes in side that folder."

Some options:

  1. Programatically doing the same I suggest opendir with readdir_r to enumarate all files -- based on its CPU and Memory.
  2. You can run du command using shell.

There are other options too ... it depend on how you want to do.

Upvotes: 2

Related Questions