Created
August 8, 2013 00:09
-
-
Save ryandevore/6180202 to your computer and use it in GitHub Desktop.
Sum all the file size under a given folder path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface NSFileManager (UUFileSizeCalculations) | |
- (unsigned long long) uuFileSizeAtPath:(NSString*)path; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "NSFileManager+UUFileSizeCalculations.h" | |
@implementation NSFileManager (UUFileSizeCalculations) | |
- (unsigned long long) uuFileSizeAtPath:(NSString*)path | |
{ | |
unsigned long long totalSize = 0; | |
BOOL isDir; | |
if ([self fileExistsAtPath:path isDirectory:&isDir]) | |
{ | |
if (isDir) | |
{ | |
NSError* err = nil; | |
NSArray* list = [self subpathsOfDirectoryAtPath:path error:&err]; | |
if (!err && list) | |
{ | |
for (NSString* subPath in list) | |
{ | |
NSString* fullSubPath = [path stringByAppendingPathComponent:subPath]; | |
err = nil; | |
NSDictionary* attrs = [self attributesOfItemAtPath:fullSubPath error:&err]; | |
if (!err && attrs) | |
{ | |
NSNumber* fileSize = [attrs valueForKey:NSFileSize]; | |
if (fileSize) | |
{ | |
totalSize += fileSize.unsignedLongLongValue; | |
} | |
} | |
} | |
} | |
} | |
} | |
return totalSize; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment