69

I have a path to file contained in an NSString. Is there a method to get its file size?

hekevintran
  • 22,822
  • 32
  • 111
  • 180

10 Answers10

138

This one liner can help people:

unsigned long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:someFilePath error:nil] fileSize];

This returns the file size in Bytes.

Flexo
  • 87,323
  • 22
  • 191
  • 272
Oded Ben Dov
  • 9,936
  • 6
  • 38
  • 53
74

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];
Frank Shearar
  • 17,012
  • 8
  • 67
  • 94
16

In case some one needs a Swift version:

let attr: NSDictionary = try! NSFileManager.defaultManager().attributesOfItemAtPath(path)
print(attr.fileSize())
Babac
  • 931
  • 11
  • 21
Tyler Liu
  • 19,552
  • 11
  • 100
  • 84
12

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);
Community
  • 1
  • 1
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
  • 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
8

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);
}
Sk Borhan Uddin
  • 813
  • 1
  • 9
  • 19
6

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]];
Apollo
  • 1,913
  • 2
  • 19
  • 26
2

Swift 2.2:

do {
    let attr: NSDictionary = try NSFileManager.defaultManager().attributesOfItemAtPath(path)
    print(attr.fileSize())
} catch {
        print(error)
}
Bill Chan
  • 3,199
  • 36
  • 32
1

It will give File size in Byte...

uint64_t fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:_filePath error:nil] fileSize];
Akshay Phulare
  • 1,359
  • 2
  • 10
  • 15
0

Swift4:

        let attributes = try! FileManager.default.attributesOfItem(atPath: path)
        let fileSize = attributes[.size] as! NSNumber
Denis Kreshikhin
  • 8,856
  • 9
  • 52
  • 84
0

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)")
}
d0ping
  • 462
  • 4
  • 16