On 7-Apr-09, at 11:08 AM, Jo Phils wrote:

My apologies if this has been answered before but isn't there a simple way to get the file size as it shows under Size in Finder without using Carbon and without enumerating the directory? My understanding is NSFileSize will not do it?


I use something like this to get the total size of the data + resource forks for a bunch of paths. If the paths contain a large number of files, you can run it on a separate thread so it doesn't block the UI.

- (void) totalSizeAndCountAt:(NSArray *)paths {
        unsigned long long totalSize = 0, fileCount = 0;
NSFileManager *fmgr = [NSFileManager new]; // on mainThread, you can use defaultManager
        NSDirectoryEnumerator *e;
        for (NSString *path in paths) {
                if ([fmgr isDirAtPath:path] && ![fmgr isSymLinkAtPath:path]) {
                        e = [fmgr enumeratorAtPath:path];
                        for (NSString *subPath in e) {
totalSize += [fmgr totalSizeAtFilePath:[path stringByAppendingPathComponent:subPath]];
                                fileCount++;
                        }
                } else {
                        totalSize = [fmgr totalSizeAtFilePath:path];
                        fileCount++;
                }
        }

// do something with fileCount and totalSize here, e.g., notify a delegate on the main thread to display the results
}

- (unsigned long long) totalSizeAtFilePath:(NSString *)path {
        unsigned long long size = 0;
        Boolean isDirectory;
        FSCatalogInfo info;
        FSRef ref;
        OSStatus status;
        OSErr err;
status = FSPathMakeRef((const unsigned char*)[path fileSystemRepresentation], &ref, &isDirectory);
        if (status  == noErr) {
err = FSGetCatalogInfo(&ref, kFSCatInfoDataSizes | kFSCatInfoRsrcSizes, &info, NULL, NULL, NULL);
                if (err  == noErr)
                        size = info.dataLogicalSize + info.rsrcLogicalSize;
        }
        return size;
}

----
Karl Moskowski <kolpa...@voodooergonomics.com>
Voodoo Ergonomics Inc. <http://voodooergonomics.com/>

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to