• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Calculating file size
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • Re: Calculating file size
      • From: email@hidden
    • Re: Calculating file size
      • From: Jens Alfke <email@hidden>
  • Prev by Date: Re: How to import drawing paths into a Cocoa application?
  • Next by Date: Re: NSBezier Artifacts
  • Previous by thread: Re: Observer CoreData Objects changes
  • Next by thread: Re: Calculating file size
  • Index(es):
    • Date
    • Thread