Finding file's size

Objective CCocoa TouchIosNsfilemanagerFilesize

Objective C Problem Overview


In my iPhone app I am using the following code to find a file's size. Even though the file exists, I am seeing zero for the size. Can anyone help me? Thanks in advance.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *URL = [documentsDirectory stringByAppendingPathComponent:@"XML/Extras/Approval.xml"];

NSLog(@"URL:%@",URL);
NSError *attributesError = nil;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError];

int fileSize = [fileAttributes fileSize];

Objective C Solutions


Solution 1 - Objective C

Try this;

NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError];

NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
long long fileSize = [fileSizeNumber longLongValue];

Note that the fileSize won't necessarily fit in an integer (especially a signed one) although you could certainly drop to a long for iOS as you'll never exceed that in reality. The example uses long long as in my code I have to be compatible with systems with much larger storage available.

Solution 2 - Objective C

One liner in Swift:

let fileSize = try! NSFileManager.defaultManager().attributesOfItemAtPath(fileURL.path!)[NSFileSize]!.longLongValue

Solution 3 - Objective C

If you have a URL (NSURL, not a String), you can get the file size without a FileManager:

 let attributes = try? myURL.resourceValues(forKeys: Set([.fileSizeKey]))
 let fileSize = attributes?.fileSize // Int?

Solution 4 - Objective C

Swift 4.x

do {
    let fileSize = try (FileManager.default.attributesOfItem(atPath: filePath) as NSDictionary).fileSize()
            print(fileSize)
    } catch let error {
            print(error)
    }

Solution 5 - Objective C

Get the file size in MB Try This code for swift

func getSizeOfFile(withPath path:String) -> UInt64?
{
    var totalSpace : UInt64?
    
    var dict : [FileAttributeKey : Any]?
    
    do {
        dict = try FileManager.default.attributesOfItem(atPath: path)
    } catch let error as NSError {
         print(error.localizedDescription)
    }
    
    if dict != nil {
        let fileSystemSizeInBytes = dict![FileAttributeKey.systemSize] as! NSNumber
       
        totalSpace = fileSystemSizeInBytes.uint64Value
        return (totalSpace!/1024)/1024
    }
    return nil
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKiranView Question on Stackoverflow
Solution 1 - Objective CRogerView Answer on Stackoverflow
Solution 2 - Objective CKrodakView Answer on Stackoverflow
Solution 3 - Objective CkelinView Answer on Stackoverflow
Solution 4 - Objective CHemangView Answer on Stackoverflow
Solution 5 - Objective CUrvish PatelView Answer on Stackoverflow