How to get size of a folder

Discussion in 'Mac Programming' started by imranpundeer, May 14, 2009.

  1. imranpundeer macrumors newbie

    Jan 10, 2009
    Hi All,
    I want to get size of a folder on MAC. is there any API relateing to thisThanks in Advance and any help would be appriciated.

  2. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    NSFileManager has some methods for individual files, getting lists of files, etc., so you might be able to put something together, but it would require diving down through every subdirectory recursively, etc.

    I would recommend just parsing the output of du... i.e.:
    du -sk /users/xxx/Music

    The output is in bytes. You could just parse the output of this program instead of writing your own implementation, essentially, with cocoa routines.

    If you want a "human-readable" version with megabytes, kilobytes, etc., you can pass -h.

  3. larkost macrumors 6502a

    Oct 13, 2007
    You are going to have to walk the tree with [NSFileManager enumeratorAtPath:], and then get use [NSFileManager fileAttributesAtPath:...] to get the size and add them up. The thing I don't know is if that will get the resource forks, metadata, etc... so you have to figure out what you need. And depending on what you are doing there might be faster routines (in Carbon or POSIX layers), and there is even one that is really fast when you are enumerating an entire drive, but they are generally more difficult to work with and only appreciably faster when you are enumerating a lot of items.
  4. imranpundeer thread starter macrumors newbie

    Jan 10, 2009
    Thanks for direction.
    Could you please specify any Cocoa routine how we can implement that in our prograam.

  5. jtibbitt macrumors regular


    Jan 3, 2009

    du -sk -----> outputs the # KB
    du -sm -----> outputs the # MB
    du -sg -----> outputs the # GB
    du -sh -----> outputs the size in a human readable form

Share This Page