5
(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;
}
VMAtm
  • 27,943
  • 17
  • 79
  • 125
Meet
  • 4,904
  • 3
  • 24
  • 39

1 Answers1

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