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];
}
Community
  • 1
  • 1
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • Response to close-vote: this question doesn't belong to superuser.com, as it is a valid programming question. it must be done in obj-c – vikingosegundo Apr 04 '10 at 13:05

1 Answers1

1

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];
}

Edit 2015:

I added a much more comprehensive (and more correct) answer to a similar question here.

Community
  • 1
  • 1
Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200