简体   繁体   中英

How to get the file size given a path?

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.

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];

In case some one needs a Swift version:

let attr: NSDictionary = try! NSFileManager.defaultManager().attributesOfItemAtPath(path)
print(attr.fileSize())

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);

根据Oded Ben Dov的回答,我宁愿在这里使用一个对象:

NSNumber * mySize = [NSNumber numberWithUnsignedLongLong:[[[NSFileManager defaultManager] attributesOfItemAtPath:someFilePath error:nil] fileSize]];

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);
}

Swift 2.2:

do {
    let attr: NSDictionary = try NSFileManager.defaultManager().attributesOfItemAtPath(path)
    print(attr.fileSize())
} catch {
        print(error)
}

它将以字节为单位给出文件大小...

uint64_t fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:_filePath error:nil] fileSize];

Swift4:

        let attributes = try! FileManager.default.attributesOfItem(atPath: path)
        let fileSize = attributes[.size] as! NSNumber

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)")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM