views:

97

answers:

1

I have stored images from the net like this

Documents/imagecache/domain1/original/path/inURI/foo.png
Documents/imagecache/domain2/original/path/inURI/bar.png
Documents/imagecache/...
Documents/imagecache/...

Now I'd like to check the size of imagecache including all it sub-directories.

Is there a convenient way of doing it — preferable without crawling through all the data manually?

edit

I want to check the size inside an iPhone App. jailbreaking is no option.

Solution

derived from Nikolai's answer

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *p = [documentsDirectory stringByAppendingPathComponent:path];
unsigned long long x =0 ;
for (NSString *s in [fileMgr subpathsAtPath:p]) {
    NSDictionary *attributes = [fileMgr attributesOfItemAtPath: [p stringByAppendingPathComponent:s] error:NULL];
    x+=[attributes fileSize];
}
+1  A: 

The information is not stored in the filesystem. There's no way to get the combined size without traversing the directories.

I’m not aware of a library function that does that but here's a quick hack that should work:

unsigned long long combinedSize = 0;
for (NSString* path in [[NSFileManager defaultManager] subpathsAtPath:myDir]) {
    combinedSize += [[attributesOfItemAtPath:path error:NULL] fileSize];
}
Nikolai Ruhe