NSFileManager Additions
Continuing with providing useful Cocoa snippets, I have posted a few NSFileManager category methods to help easily determine the file size of a single file, directory, or quickly hide a file/directory using the invisibility bit (like using /usr/bin/setfile, ie: setfile -a V somefile).
1 2 3 4 5 | @interface NSFileManager (MacFanatic) - (unsigned long) sizeOfDirectory:(NSString*)dir; // Bytes - (unsigned long) sizeOfFile:(NSString*)file; // Bytes - (BOOL)setInvisibilityFlag:(BOOL)invisible forPath:(NSString*)path; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | - (unsigned long) sizeOfDirectory:(NSString*)dir { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSFileManager* manager = [NSFileManager defaultManager]; unsigned long size = 0; NSDirectoryEnumerator *e = [manager enumeratorAtPath:dir]; NSString* file = nil; while ( file = [e nextObject] ) { size += [manager sizeOfFile:[dir stringByAppendingPathComponent:file]]; } [pool release]; return size; } |
Download
Be sure to download a zip of the .h and .m files (10.4+)
December 5, 2009 9:45 am
Add a comment














Submitting Your Comment, give me a second...