Re: Calculating file size
Re: Calculating file size
- Subject: Re: Calculating file size
- From: email@hidden
- Date: Sun, 27 Apr 2008 18:39:04 -0400
Others have answered with good suggestions for other APIs, but I will
point out for the record that you can do it in Cocoa, too, because the
file system has a path-based mechanism in which "..namedfork/rsrc" is
appended to the path. For example, in Terminal:
$ ls -li Documents//Example.doc
108 -rw-r--r--@ 1 aburgh aburgh 23552 Apr 27 2006 Documents/
Example.doc
$ ls -li Documents/Example.doc/..namedfork/rsrc
108 -rw-r--r-- 1 aburgh aburgh 286 Apr 27 2006 Documents/
Example.doc/..namedfork/rsrc
Notice that the "inode" is the same (the Catalog Node ID on HFS+), but
size reflects the different forks. You can use this technique from
any program that lets you specify a path, such as command line
utilities, and you can even read and write the contents of the forks
this way. This is documented in the Mac OS X system documentation.
In your case, you would need to construct the path and use
NSFileManager's fileAttributesAtPath to get the attributes for the
resource fork.
Aaron
On Apr 26, 2008, at 9:50 PM, Cocoa Dev wrote:
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
_______________________________________________
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