I have a path to file contained in an NSString. Is there a method to get its file size?
10 Answers
This one liner can help people:
unsigned long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:someFilePath error:nil] fileSize];
This returns the file size in Bytes.
![](../../users/profiles/168175.webp)
- 82,006
- 22
- 174
- 256
![](../../users/profiles/380316.webp)
- 8,892
- 6
- 29
- 45
-
2I like this one. But what measurement is this in? bytes, Kb, etc? Thanks too. – James Jan 03 '12 at 13:55
-
7bytes - the measurement is bytes – Oded Ben Dov Jan 11 '12 at 09:18
-
What happens if your file is larger than `INT_MAX` bytes in size? You might want to cast the result to `size_t` or `unsigned long long int`, which allows accurate reporting of the size of large files (> 2 GB). – Alex Reynolds Jan 12 '12 at 12:13
-
3The actual return value of the method is `unsigned long long`, so `int` isn't fit to be here. – coverback Jun 27 '12 at 08:06
Bear in mind that fileAttributesAtPath:traverseLink: is deprecated as of Mac OS X v10.5. Use attributesOfItemAtPath:error:
instead, described at the same URL thesamet mentions.
With the caveat that I'm an Objective-C newbie, and I'm ignoring errors that might occur in calling attributesOfItemAtPath:error:
, you can do the following:
NSString *yourPath = @"Whatever.txt";
NSFileManager *man = [NSFileManager defaultManager];
NSDictionary *attrs = [man attributesOfItemAtPath: yourPath error: NULL];
UInt32 result = [attrs fileSize];
![](../../users/profiles/10259.webp)
- 16,631
- 8
- 63
- 90
-
2This code leaks the alloced FileManager. I recommend you simply use the NSFileManager.defaultManager Singleton to avoid this. – Johannes Rudolph Oct 18 '12 at 10:07
In case some one needs a Swift version:
let attr: NSDictionary = try! NSFileManager.defaultManager().attributesOfItemAtPath(path)
print(attr.fileSize())
![](../../users/profiles/2798882.webp)
- 741
- 10
- 21
![](../../users/profiles/862862.webp)
- 16,201
- 9
- 87
- 75
CPU raises with attributesOfItemAtPath:error:
You should use stat.
#import <sys/stat.h>
struct stat stat1;
if( stat([inFilePath fileSystemRepresentation], &stat1) ) {
// something is wrong
}
long long size = stat1.st_size;
printf("Size: %lld\n", stat1.st_size);
![](../../users/profiles/-1.webp)
- 1
- 1
![](../../users/profiles/944634.webp)
- 22,143
- 7
- 65
- 138
-
Shouldn't you be using fileSystemRepresentation rather than UTF8String here? – David Knight Jan 23 '13 at 11:41
-
You are right. HFS+ defines a standard Unicode decomposition ("canonical decomposition") for filenames. -UTF8String is not guaranteed to return the proper composition; -fileSystemRepresentation is.[1](http://cocoadev.com/wiki/StringWithCString) – Parag Bafna Jan 28 '13 at 07:11
-
@ParagBafna I know this is an old thread but do you know how I could use the `stat` structure in swift? – Jonathan H. Jun 26 '15 at 22:05
If you want only file size with bytes just use,
unsigned long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:yourAssetPath error:nil] fileSize];
NSByteCountFormatter string conversion of filesize (from Bytes) with precise KB, MB, GB ... Its returns like 120 MB
or 120 KB
NSError *error = nil;
NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:yourAssetPath error:&error];
if (attrs) {
NSString *string = [NSByteCountFormatter stringFromByteCount:fileSize countStyle:NSByteCountFormatterCountStyleBinary];
NSLog(@"%@", string);
}
![](../../users/profiles/797196.webp)
- 763
- 1
- 8
- 17
Following the answer from Oded Ben Dov, I would rather use an object here:
NSNumber * mySize = [NSNumber numberWithUnsignedLongLong:[[[NSFileManager defaultManager] attributesOfItemAtPath:someFilePath error:nil] fileSize]];
![](../../users/profiles/1151052.webp)
- 1,625
- 2
- 17
- 25
Swift 2.2:
do {
let attr: NSDictionary = try NSFileManager.defaultManager().attributesOfItemAtPath(path)
print(attr.fileSize())
} catch {
print(error)
}
![](../../users/profiles/353911.webp)
- 2,416
- 27
- 26
It will give File size in Byte...
uint64_t fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:_filePath error:nil] fileSize];
![](../../users/profiles/5467417.webp)
- 1,125
- 2
- 9
- 15
Swift4:
let attributes = try! FileManager.default.attributesOfItem(atPath: path)
let fileSize = attributes[.size] as! NSNumber
![](../../users/profiles/1062636.webp)
- 6,887
- 6
- 45
- 75
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)")
}
![](../../users/profiles/3501025.webp)
- 442
- 4
- 15