Re: Get size of folder
Re: Get size of folder
- Subject: Re: Get size of folder
- From: PCWiz <email@hidden>
- Date: Wed, 19 Aug 2009 09:29:01 -0600
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
On 2009-08-19, at 12:49 AM, Jerry Krinock wrote:
On 2009 Aug 18, at 21:17, PCWiz wrote:
Hi,
I need a good method to find the size of a file or folder exactly
as displayed in Finder. I've tried every method I could find on the
internet, from using the du shell utility with NSTask to using the
Carbon file manager. I need something that will work under heavy
load (processing hundreds or thousands of files). The methods I've
tried, some of them work but fail under load. Others just don't
return a accurate file size, or they work for some files/folders
and mess up on others. What's the easiest way to go about doing this?
It would be interesting to see all the methods you've tried, or at
least the one that came closest to working. Off hand, I would think
that stat(2) would be the tool to use, and probably File Manager
uses stat(2) under the hood. You might post this over on email@hidden
if you don't get an answer on cocoa-dev.
_______________________________________________
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