目标c:如何只删除目录下的所有文件,但保留目录本身
我发现下面的代码删除了objective-c中的文件,但是我只想删除Caches
目录下的所有文件并保留目录Caches
本身。
有人可以建议这样做的方法吗?
谢谢
NSFileManager *filemgr; filemgr = [NSFileManager defaultManager]; if ([filemgr removeItemAtPath: [NSHomeDirectory() stringByAppendingString:@"/Library/Caches"] error: NULL] == YES) NSLog (@"Remove successful"); else NSLog (@"Remove failed");
更新
NSFileManager *filemgr; filemgr = [NSFileManager defaultManager]; if ([filemgr removeItemAtPath: [NSHomeDirectory() stringByAppendingString:@"/Library/Caches"] error: NULL] == YES) NSLog (@"Remove successful"); else NSLog (@"Remove failed"); [filemgr createDirectoryAtPath: [NSHomeDirectory() stringByAppendingString:@"/Library/Caches"] withIntermediateDirectories:NO attributes:nil error:nil];
循环遍历该目录中的文件。
NSFileManager *fileMgr = [NSFileManager defaultManager]; NSArray *fileArray = [fileMgr contentsOfDirectoryAtPath:directory error:nil]; for (NSString *filename in fileArray) { [fileMgr removeItemAtPath:[directory stringByAppendingPathComponent:filename] error:NULL]; }
- (void) removeDocuments { NSString *docDir = // get documents directory NSString *cacheDir = [docDir stringByAppendingPathComponent: @"cacheDir"]; // check if cache dir exists // get all files in this directory NSFileManager *fm = [NSFileManager defaultManager]; NSArray *fileList = [fm contentsOfDirectoryAtPath: cacheDir error: nil]; // remove for(NSInteger i = 0; i < [fileList count]; ++i) { NSString *fp = [fileList objectAtIndex: i]; NSString *remPath = [cacheDir stringByAppendingPathComponent: fp]; [fm removeItemAtPath: remPath error: nil]; } }
- 在后台加载图像以优化ios中的加载
- 生成崩溃而不是.crash文件的“JetsamEvent-2015-03-18-151504.ips”文件
- iOS 10问题:即使设置了ContentSize,UIScrollView也不滚动
- 在NSDictionary中<从数值中提取数据失败>
- 如何在obj c中调用一个类的方法到另一个类
- 自定义的UITableViewCell给了我:这个类不是关键值编码兼容
- 如何在UIAlertView(iOS)中的其他两个button(堆叠)之间添加Cancelbutton
- 如何修复似乎不影响自定义UITableViewCell布局的奇怪的NSLayoutConstraint错误
- 对<TSActivityMapVC:0x81b1000>开始/结束外观转换的不平衡调用