Skip to content

Instantly share code, notes, and snippets.

@demonnico
Created December 3, 2013 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save demonnico/7764414 to your computer and use it in GitHub Desktop.
Save demonnico/7764414 to your computer and use it in GitHub Desktop.
get totally files size in directory
@impelementaton NSString(Utils)
+ (uint64_t)sizeAtPath:(NSString *)filePath diskMode:(BOOL)diskMode
{
uint64_t totalSize = 0;
NSMutableArray *searchPaths = [NSMutableArray arrayWithObject:filePath];
while ([searchPaths count] > 0)
{
@autoreleasepool
{
NSString *fullPath = [searchPaths objectAtIndex:0];
[searchPaths removeObjectAtIndex:0];
struct stat fileStat;
if (lstat([fullPath fileSystemRepresentation], &fileStat) == 0)
{
if (fileStat.st_mode & S_IFDIR)
{
NSArray *childSubPaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:fullPath error:nil];
for (NSString *childItem in childSubPaths)
{
NSString *childPath = [fullPath stringByAppendingPathComponent:childItem];
[searchPaths insertObject:childPath atIndex:0];
}
}else
{
if (diskMode)
totalSize += fileStat.st_blocks*512;
else
totalSize += fileStat.st_size;
}
}
}
}
return totalSize;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment