Search code examples
objective-cmacoscocoansfilemanager

How to get Size and free space of a directory in cocoa?


I want to access the actual size of a directory, and also free space available in a directory. I already used [NSFileManager defaultManager]attributesOfItemAtPath:path error:&error] method. This method works fine for files but for directory, it does not provide actual value. Please help to solve this problem. Thanks in advance.


Solution

  • You can calculate the size of a directory using this particular method, the fastest, using Carbon, instead of NSEnumerator : here

    To calculate free space, you could use that method. Make sure you enter the full path of the volume :

    NSDictionary* fileAttributes = [[NSFileManager defaultManager] fileSystemAttributesAtPath:folder];

    unsigned long long size = [[fileAttributes objectForKey:NSFileSystemFreeSize] longLongValue];

    Where size is what you're looking for.