0

From the Mac Finder app I know that my file PicViewer.xcodeproj has a files size of 35,697 bytes. The code below gives me a file size of 160. I know the code works as it correctly tells me the file size of an Excel file.

I used the code from this stackoverflow question How can i get my file size in Objective-C with a minor change.

NSString *directoryPath = [@"~" stringByExpandingTildeInPath];
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *array = [fm contentsOfDirectoryAtPath: directoryPath error:NULL];
for (NSString *filename in array) {
    NSError *attributesError = nil;
    NSDictionary *attributes = [fm attributesOfItemAtPath:[directoryPath stringByAppendingPathComponent:filename]
                                                    error:&attributesError];
    unsigned long long size = [attributes fileSize];
    NSString *filetype = [attributes fileType];
    NSLog(@"%@ %@ %llu", filename, filetype, size);
}

It also reports that the file type is NSFileTypeDirectory and not NSFileTypeRegular (and I suspect this is causing the file size to be reported incorrectly). For example:

Exercise Record.xlsx NSFileTypeRegular 637131

PicViewer.xcodeproj NSFileTypeDirectory 160

Any help would be greatly appreciated.

Keith
  • 59
  • 6

1 Answers1

1

xcodeproj is a directory indeed, you can expand its contents in Finder using Show package content (or whatever, I do not use English MacOS) menu command, so in you want to calculate its size, you should do it recursively, for instance like that

schmidt9
  • 4,436
  • 1
  • 26
  • 32
  • Thanks, this helped. Is there anyway to distinguish between folders and package directories? – Keith Dec 30 '18 at 14:27
  • @Keith I do not think so, it is really just an ordinary directory which mimics to a file having a file-like dot-separated extension and an according app association to handle this – schmidt9 Dec 31 '18 at 09:30
  • 1
    getResourceValue: forKey: NSURLIsPackageKey worked for me in being able to tell the difference between a regular folder and a "package" folder packages from regular folders. – Keith Jan 03 '19 at 01:20