计算缓存文件夹的大小以及删除该清除文件夹

、、、

//获取文件夹大小
NSFileManager *mgr = [NSFileManager defaultManager];
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
//文件夹的属性
//NSDictionary *attrs = [mgr attributesOfItemAtPath:caches error:nil];
//获得文件夹的属性,文件夹没有大小遍历所有文件加起来活儿文件的大小
//获取caches里面的所有内容
//NSArray *contents =  [mgr contentsOfDirectoryAtPath:caches error:nil];
NSArray *subPaths = [mgr subpathsAtPath:caches];
NSInteger totalByteSize = 0;
for (NSString *subPath in subPaths)
{
    NSString *fullPath = [caches stringByAppendingPathComponent:subPath];
    //判断是文件还是文件夹
    BOOL dir = NO;
    [mgr fileExistsAtPath:fullPath isDirectory:&dir];
    if (dir == NO)
    {
        //文件
        NSDictionary *attrs = [mgr attributesOfItemAtPath:fullPath error:nil];
        NSInteger byteSize = [attrs[NSFileSize] integerValue];
        
        totalByteSize += byteSize;
        
    }
}
NSLog(@"%ld",totalByteSize);

、、、
清缓存
、、、
//删除该文件夹
[mgr removeItemAtPath:caches error:nil];
、、、

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容