• 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: finder file size
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: finder file size


  • Subject: Re: finder file size
  • From: Karl Moskowski <email@hidden>
  • Date: Tue, 7 Apr 2009 12:09:28 -0400


On 7-Apr-09, at 11:08 AM, Jo Phils wrote:

My apologies if this has been answered before but isn't there a simple way to get the file size as it shows under Size in Finder without using Carbon and without enumerating the directory? My understanding is NSFileSize will not do it?


I use something like this to get the total size of the data + resource forks for a bunch of paths. If the paths contain a large number of files, you can run it on a separate thread so it doesn't block the UI.

- (void) totalSizeAndCountAt:(NSArray *)paths {
unsigned long long totalSize = 0, fileCount = 0;
NSFileManager *fmgr = [NSFileManager new]; // on mainThread, you can use defaultManager
NSDirectoryEnumerator *e;
for (NSString *path in paths) {
if ([fmgr isDirAtPath:path] && ![fmgr isSymLinkAtPath:path]) {
e = [fmgr enumeratorAtPath:path];
for (NSString *subPath in e) {
totalSize += [fmgr totalSizeAtFilePath:[path stringByAppendingPathComponent:subPath]];
fileCount++;
}
} else {
totalSize = [fmgr totalSizeAtFilePath:path];
fileCount++;
}
}


// do something with fileCount and totalSize here, e.g., notify a delegate on the main thread to display the results
}


- (unsigned long long) totalSizeAtFilePath:(NSString *)path {
unsigned long long size = 0;
Boolean isDirectory;
FSCatalogInfo info;
FSRef ref;
OSStatus status;
OSErr err;
status = FSPathMakeRef((const unsigned char*)[path fileSystemRepresentation], &ref, &isDirectory);
if (status == noErr) {
err = FSGetCatalogInfo(&ref, kFSCatInfoDataSizes | kFSCatInfoRsrcSizes, &info, NULL, NULL, NULL);
if (err == noErr)
size = info.dataLogicalSize + info.rsrcLogicalSize;
}
return size;
}


----
Karl Moskowski <email@hidden>
Voodoo Ergonomics Inc. <http://voodooergonomics.com/>

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

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: finder file size
      • From: Rosyna <email@hidden>
  • Prev by Date: Invalid strings
  • Next by Date: Re: SQLite and Unicode
  • Previous by thread: Re: finder file size
  • Next by thread: Re: finder file size
  • Index(es):
    • Date
    • Thread