How to get size of a folder

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

  1. imranpundeer macrumors newbie

    Joined:
    Jan 10, 2009
    #1
    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.

    Regards,
    Imran
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    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
    output:
    23287396

    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.

    -Lee
     
  3. larkost macrumors 6502a

    Joined:
    Oct 13, 2007
    #3
    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

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

    Thanks.
     
  5. jtibbitt macrumors regular

    jtibbitt

    Joined:
    Jan 3, 2009
    #5

    Actually,
    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