• 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
Re: Get size of folder
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Get size of folder


  • Subject: Re: Get size of folder
  • From: Jean-Daniel Dupas <email@hidden>
  • Date: Wed, 19 Aug 2009 18:01:06 +0200

Le 19 août 2009 à 17:29, PCWiz a écrit :

Hi,

It would probably be a good idea to tell you guys some of the methods I have tried. First of all, I've tried just using NSFileManager and NSEnumerator to enumerate through the directory. This didn't add up resource forks AND it didn't round up the sizes to Finder's 4KB minimum block size so it was obviously inaccurate. I also tried using the du -sk shell command (which returned folder size in KB) with NSTask, and while it was mostly accurate, it collapsed under heavy load. The next thing I tried was using the Carbon File Manager API using this method by Dave DeLong:

http://github.com/davedelong/BuildCleaner/blob/b2712242b4eea1fff0e78a08b393a417e3019c8a/NSFileManager+FileSize.m

This method worked for some folders/files, but for others it returned wildly innacurate numbers. The method I'm using right now is probably the closest I've ever gotten:

#define MINBLOCK 4096

+ (long long)sizeOfFile:(NSString *)filePath
{
long long fileSize = 0;
// Check if item is file or folder
if ([[[[NSFileManager defaultManager] fileAttributesAtPath:filePath traverseLink:NO] objectForKey:NSFileType] isEqualTo:@"NSFileTypeRegular"]) {
fileSize = ((([[[NSFileManager defaultManager] fileAttributesAtPath:filePath traverseLink:YES] fileSize] + MINBLOCK - 1) / MINBLOCK) * MINBLOCK);
} else {
NSString *file;
NSNumber *fsize;
NSDictionary *fattrs;
NSDirectoryEnumerator *de;
de = [[NSFileManager defaultManager] enumeratorAtPath:filePath];
while(file = [de nextObject]) {
rsrcPath = [NSString stringWithFormat:@"%@/..namedfork/rsrc", file];
fattrs = [de fileAttributes];
fsize = [fattrs objectForKey:NSFileSize];
if (![[fattrs valueForKey:NSFileType] isEqualTo:NSFileTypeDirectory]) {
fileSize += ((([[fattrs valueForKey:NSFileSize] longLongValue] + MINBLOCK - 1) / MINBLOCK) * MINBLOCK);
}
}
}
return fileSize;
}


As you can see it performs a quick check to see whether its a file or a folder, then takes the appropriate actions to find the file size. The folder size method rounds up the files to the 4KB minimum block size. I think what this method needs is just to add up the resource forks and then it would be totally accurate. I'm just confused as to how I would do that.

Thanks

Use The Core Services File Manager API :

http://developer.apple.com/documentation/Performance/Conceptual/FileSystem/Articles/IteratingFiles.html


_______________________________________________

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: Get size of folder
      • From: Dave DeLong <email@hidden>
References: 
 >Get size of folder (From: PCWiz <email@hidden>)
 >Re: Get size of folder (From: PCWiz <email@hidden>)

  • Prev by Date: Re: FREED message is crashing my program
  • Next by Date: Re: FREED message is crashing my program
  • Previous by thread: Re: Get size of folder
  • Next by thread: Re: Get size of folder
  • Index(es):
    • Date
    • Thread