I have a path to file contained in an NSString. Is there a method to get its file size?
This one liner can help people:
unsigned long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:someFilePath error:nil] fileSize];
This returns the file size in Bytes.
In Swift 3.x and above you can use:
do {
//return [FileAttributeKey : Any]
let attr = try FileManager.default.attributesOfItem(atPath: filePath)
fileSize = attr[FileAttributeKey.size] as! UInt64
//or you can convert to NSDictionary, then get file size old way as well.
let attrDict: NSDictionary = try FileManager.default.attributesOfItem(atPath: filePath) as NSDictionary
fileSize = dict.fileSize()
} catch {
print("Error: \(error)")
}
Swift4:
let attributes = try! FileManager.default.attributesOfItem(atPath: path)
let fileSize = attributes[.size] as! NSNumber
Swift 2.2:
do {
let attr: NSDictionary = try NSFileManager.defaultManager().attributesOfItemAtPath(path)
print(attr.fileSize())
} catch {
print(error)
}