Skip to content

Instantly share code, notes, and snippets.

@ryandevore
Created August 8, 2013 00:09
Show Gist options
  • Save ryandevore/6180202 to your computer and use it in GitHub Desktop.
Save ryandevore/6180202 to your computer and use it in GitHub Desktop.
Sum all the file size under a given folder path
#import <Foundation/Foundation.h>
@interface NSFileManager (UUFileSizeCalculations)
- (unsigned long long) uuFileSizeAtPath:(NSString*)path;
@end
#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