删除沙盒指定格式文件和获取沙盒总容量大小
2017-07-18 11:26
375 查看
通过以下代码可以读取到文件夹里面的文件
#define kDocument_Folder [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
NSFileManager *fM = [NSFileManager defaultManager];
NSArray *fileList = [fM contentsOfDirectoryAtPath:kDocument_Folder error:nil];
NSString *path;
NSString *extension;
for(NSString *file in fileList) {
path = [kDocument_Folder stringByAppendingPathComponent:file];
extension = [path pathExtension];
//以下是通过判断文件后缀,如果是图片格式就删除掉
if (([extension compare:@"jpg" options:NSCaseInsensitiveSearch] == NSOrderedSame)||([extensioncompare:@"png" options:NSCaseInsensitiveSearch] == NSOrderedSame)||([extension compare:@"jpeg"options:NSCaseInsensitiveSearch] == NSOrderedSame)) {
[fM removeItemAtPath:path error:nil];
}
}
还有以下的代码可以计算文件的总容量
const unsigned int bytes = 1024*1024 ;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setPositiveFormat:@"##0.00"];
float size;
//NSString *extension;
for(NSString *file in fileList) {
path = [kDocument_Folder stringByAppendingPathComponent:file];
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
size=size+(float)[fileAttributes fileSize];
}
NSNumber *total = [NSNumber numberWithFloat:(size/bytes)];
NSLog(@"%@",[NSString stringWithFormat:@"占用容量:%@ MB",[formatter stringFromNumber:total]]);
#define kDocument_Folder [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
NSFileManager *fM = [NSFileManager defaultManager];
NSArray *fileList = [fM contentsOfDirectoryAtPath:kDocument_Folder error:nil];
NSString *path;
NSString *extension;
for(NSString *file in fileList) {
path = [kDocument_Folder stringByAppendingPathComponent:file];
extension = [path pathExtension];
//以下是通过判断文件后缀,如果是图片格式就删除掉
if (([extension compare:@"jpg" options:NSCaseInsensitiveSearch] == NSOrderedSame)||([extensioncompare:@"png" options:NSCaseInsensitiveSearch] == NSOrderedSame)||([extension compare:@"jpeg"options:NSCaseInsensitiveSearch] == NSOrderedSame)) {
[fM removeItemAtPath:path error:nil];
}
}
还有以下的代码可以计算文件的总容量
const unsigned int bytes = 1024*1024 ;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setPositiveFormat:@"##0.00"];
float size;
//NSString *extension;
for(NSString *file in fileList) {
path = [kDocument_Folder stringByAppendingPathComponent:file];
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
size=size+(float)[fileAttributes fileSize];
}
NSNumber *total = [NSNumber numberWithFloat:(size/bytes)];
NSLog(@"%@",[NSString stringWithFormat:@"占用容量:%@ MB",[formatter stringFromNumber:total]]);
相关文章推荐
- 读取沙盒的文件,删除指定格式文件和总容量
- 获取文件夹大小以及删除指定目录下文件及目录
- 牛客网Java刷题知识点之File对象常用功能:获取文件名称、获取文件路径、获取文件大小、获取文件修改时间、创建与删除、判断、重命名、查看系统根目录、容量获取、获取某个目录下内容、过滤器
- MFC获取文件夹下指定格式的文件并输出文件名及文件大小
- 获取某文件沙盒路径 删除对应文件 获取文件夹总大小
- 使用CMD模式批量删除指定目录(支持目录中多层文件夹嵌套)下指定格式文件
- iOS 获取沙盒文件夹文件的大小
- 【Java API】java获取文件大小的方法&java删除所有文件
- linux shell 脚本 历史文件清理脚本,按天,按月,清理前N天的历史文件,删除指定大小历史文件,历史文件归档清理
- ios 获取指定路径文件大小
- 一个获取指定目录下一定格式的文件名称和文件修改时间并保存为文件的python脚本
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)(一)
- android java file 清理垃圾获取文件大小 删除文件等操作
- iOS 获取文件大小fileSize && 文件删除
- Linux 脚本删除大于指定大小的文件
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)
- 获取沙盒文件Cache文件内的文件夹的大小
- Android清除外部和内部缓存,数据库,SharedPreference,指定路径下的文件,获取缓存大小,格式化储存单位工具类
- dos下快速获取文件数量并删除小于指定数量的文件夹