(NSString *)
getApplicationUsage{
double directorySizeInBytes = 0.0f;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES);
NSString *pathStr = [paths objectAtIndex:0];
pathStr = [pathStr stringByDeletingLastPathComponent]; //REMOVE THE LAST PATH COMPONENT i.e /Applications
NSDirectoryEnumerator *enumrator = [[NSFileManager defaultManager] enumeratorAtPath:pathStr];
for (NSString *itemPath in enumrator) {
itemPath = [pathStr stringByAppendingPathComponent:itemPath];
NSDictionary *attr = [[NSFileManager defaultManager] attributesOfItemAtPath:itemPath error:nil];
directorySizeInBytes = directorySizeInBytes + [[attr objectForKey:NSFileSize] doubleValue];
}
NSString *applicationUsage = [NSString stringWithFormat:@"%0.0f MB",directorySizeInBytes /1000000];
return applicationUsage;
}
Asked
Active
Viewed 1,821 times
5
-
You can try this with any folder u want.. Just append the folder name before the enumrator declaration.. – Meet Sep 15 '11 at 07:29
-
2refer http://stackoverflow.com/questions/2188469/calculate-the-size-of-a-folder – Nikunj Jadav Sep 15 '11 at 07:33
1 Answers
3
How about this?
- (unsigned long long) sizeOfFolderAtPath:(NSString *)path {
NSArray *files = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:path error:nil];
NSEnumerator *enumerator = [files objectEnumerator];
NSString *fileName;
unsigned long long size = 0;
while (fileName = [enumerator nextObject]) {
size += [[[NSFileManager defaultManager] fileAttributesAtPath:[folderPath stringByAppendingPathComponent:fileName] traverseLink:YES] fileSize];
}
return size;
}

Michael Frederick
- 16,664
- 3
- 43
- 58
-
put a counter to check whether each and every file is counted or not??? Bcoz as far I knw the call [enumerator nextObject] will skip the first file and the last file will also be skipped for counting... So the size won't be exact....btw thnk u for reviewing... – Meet Sep 17 '11 at 13:52
-
NSEnumerator does not skip objects, the point is to enumerate through the whole collection. My method will cover the first and last files. – Michael Frederick Sep 17 '11 at 19:22