Search code examples
objective-ccocoafilesizensfilemanager

NSFileManager FileSize Problem - Cocoa OSX


I have a function that checks the size of several plist files in the /User/Library/Preferences/ directory. For testing purposes, I'm using iTunes, which on my machine has a preference file of ~500kb.

EDIT: I have corrected my code as per the answer - as posted, this code works correctly.

    NSString *obj = @"iTunes";
    NSString *filePath = [NSString stringWithFormat:@"/Applications/%@.app",obj];
    NSString *bundle = [[NSBundle bundleWithPath:filePath] bundleIdentifier];

    NSString *PropertyList=[NSString stringWithFormat:@"/Preferences/%@.plist",bundle];
    NSString* fileLibraryPath = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingString:PropertyList];

    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:fileLibraryPath];

    if (fileExists) {
        NSError *err = nil;
        NSDictionary *fattrib = [[NSFileManager defaultManager] attributesOfItemAtPath:fileLibraryPath error:&err];

        if (fattrib != nil){            
            //Here I perform my comparisons

            NSLog(@"%i %@", [fattrib fileSize],obj);

        }
    }

However, no matter what I do, the size is returned as 102. Not 102kb, just 102. I have used objectForKey:NSFileSize, I have used stringValue, all 102.


As stated in the selected answer below lesson learned is to always check the path you're submitting to NSFileManager.

Thanks!


Solution

  • The filePath that you are using in

    NSDictionary *fattrib = [ ... attributesOfItemAtPath:filePath error:&err];
    

    appears to be

    /Applications/iTunes.app
    

    which on my system is a directory of size 102 bytes, same for /Applications/Mail.app - 102 bytes. Is it just that the path is not what you intend?