Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

imranpundeer

macrumors newbie
Original poster
Jan 10, 2009
25
0
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
 
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
 
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.
 
Thanks for direction.
Could you please specify any Cocoa routine how we can implement that in our prograam.

Thanks.
 
du -sk /users/xxx/Music
output:
23287396

The output is in bytes.

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

-Lee


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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.