Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get my file size in Objective-C [closed]

Tags:

objective-c

My problem:

NSString *path = @"~/Desktop/folder/";
pathg = [path stringByExpandTitlePath];
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *array = [fm contentsOfDirectoryAtPath: path error: NULL];

for(int i = 0; i < array.count; ++i)
{
    NSFileSize *num = [array[i] fileSize];
}

error: use of undeclared identifier 'num'

like image 855
user2214609 Avatar asked Apr 01 '13 11:04

user2214609


2 Answers

If you need to get the file size of a single file, you can do it by constructing a file URL to it and querying the URL's attributes directly.

NSString *filePath = [@"~/.bash_history" stringByExpandingTildeInPath];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
NSNumber *fileSizeValue = nil;
NSError *fileSizeError = nil;
[fileURL getResourceValue:&fileSizeValue
                   forKey:NSURLFileSizeKey
                    error:&fileSizeError];
if (fileSizeValue) {
    NSLog(@"value for %@ is %@", fileURL, fileSizeValue);
}
else {
    NSLog(@"error getting size for url %@ error was %@", fileURL, fileSizeError);
}

If you need to iterate contents of a directory, as in your question, you can do a URL based version as well as the way in your question. Using contentsOfDirectoryAtPath:... returns an array of file names as NSStrings, and you have to rebuild the full path and get the attributes as an additional step:

    NSString *directoryPath = [@"~" stringByExpandingTildeInPath];
    NSFileManager *fm = [NSFileManager defaultManager];
    NSArray *array = [fm contentsOfDirectoryAtPath: directoryPath error:NULL];
    for (NSString *filename in array) {
        NSError *attributesError = nil;
        NSDictionary *attributes = [fm attributesOfItemAtPath:[directoryPath stringByAppendingPathComponent:filename]
                                                        error:&attributesError];
        unsigned long long size = [attributes fileSize]; //Note this isn't a pointer
        NSLog(@"%llu", size);
    }

You can make a similar solution using file URLs:

    NSString *directoryPath = [@"~" stringByExpandingTildeInPath];
    NSURL *directoryURL = [NSURL fileURLWithPath:directoryPath
                                     isDirectory:YES];
    NSFileManager *fm = [NSFileManager defaultManager];
    NSError *contentsError = nil;
    NSArray *contents = [fm contentsOfDirectoryAtURL:directoryURL
                          includingPropertiesForKeys:@[NSURLFileSizeKey, NSURLIsDirectoryKey]
                                             options:0
                                               error:&contentsError];
    if (contents) {
        for (NSURL *contentURL in contents) {
            NSError *isDirectoryError = nil;
            NSNumber *isDirectoryNumber = nil;
            [contentURL getResourceValue:&isDirectoryNumber
                                  forKey:NSURLIsDirectoryKey
                                   error:&isDirectoryError];
            if (isDirectoryNumber) {
                if (![isDirectoryNumber boolValue]) {
                    NSNumber *fileSizeNumber = nil;
                    NSError *sizeError = nil;
                    [contentURL getResourceValue:&fileSizeNumber
                                          forKey:NSURLFileSizeKey
                                           error:&sizeError];
                    if (fileSizeNumber) {
                        NSInteger size = [fileSizeNumber integerValue];
                        NSLog(@"%li", (long)size);
                    }
                    else {
                        NSLog(@"error getting file size for file %@ error:%@",contentURL,sizeError);
                    }
                }
            }
            else {
                NSLog(@"error getting is url %@ was directory: %@", contentURL, isDirectoryError);
            }
        }
    }
    else {
        NSLog(@"error getting contents for directory %@ error: %@", directoryURL, contentsError);
    }
like image 99
Carl Veazey Avatar answered Nov 01 '22 05:11

Carl Veazey


Try this one:

NSString *yourPath=[@"~/Desktop/folder/abc.png" stringByExpandingTildeInPath];
NSFileManager *fileManager=[NSFileManager defaultManager];
NSDictionary *attributesDict=[fileManager attributesOfItemAtPath:yourPath error:NULL];
NSInteger fileSize=[attributesDict fileSize];

NSLog(@"%ld",fileSize);
like image 39
Anoop Vaidya Avatar answered Nov 01 '22 04:11

Anoop Vaidya