Calculating file size
Calculating file size
- Subject: Calculating file size
- From: "Cocoa Dev" <email@hidden>
- Date: Sat, 26 Apr 2008 18:50:12 -0700
Hello,
I was wondering what was the best way to calucate folder size with cocoa? I
was able to do this but it does not include resource forks:
#import <Foundation/Foundation.h>
int main() {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *path = [@"/Folder" stringByExpandingTildeInPath];
NSDirectoryEnumerator *e = [[NSFileManager defaultManager] enumeratorAtPath
:path];
NSString *file;
unsigned long long totalSize = 0;
while ((file = [e nextObject])) {
NSDictionary *attributes = [e fileAttributes];
NSNumber *fileSize = [attributes objectForKey:NSFileSize];
totalSize += [fileSize longLongValue];
}
printf("Total size: %lld\n", totalSize);
[pool release];
}
or from the example in documentation
- (IBAction)calculateSize:(id)sender { NSFileManager *fileManager =
[NSFileManager defaultManager]; NSString *path = @"/Folder"; NSDictionary
*fileAttributes = [fileManager fileAttributesAtPath:path traverseLink:YES];
if (fileAttributes != nil) { NSNumber *fileSize; if (fileSize =
[fileAttributes objectForKey:NSFileSize]) { NSLog(@"File size: %qi\n",
[fileSize unsignedLongLongValue]); [label setIntValue:[fileSize
unsignedLongLongValue]]; } }
But I was unsure on how to traverse the entire file tree within that
directory, and give the same size results as the finder.
Thanks,
-Mike
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden