开发者

How to retrieve Directories size including all sub-directories?

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 stringByAppendingPathC开发者_JAVA百科omponent: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];
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜