delete Files and Folders(删除文件、文件夹)
来源:互联网 发布:linux cut命令详解 编辑:程序博客网 时间:2024/05/29 10:10
1。实现方法:
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error
e.g.
@property (nonatomic, strong) NSFileManager *fileManager;
/* 根据给定的路径创建文件夹 */- (void) createFolder:(NSString *)paramPath{
NSError *error = nil;
if ([self.fileManager createDirectoryAtPath:paramPath withIntermediateDirectories:YES
attributes:nil error:&error] == NO){
NSLog(@"Failed to create folder %@. Error = %@", paramPath, error);
}
else {
NSLog(@"create folder:%@ in succeed",paramPath);
}
}
/* 在指定的路径下创建文件(在指定的文件夹里创建文件) */
- (void) createFilesInFolder:(NSString *)paramPath{
for (NSUInteger counter = 0; counter < 5; counter++){
NSString *fileName = [NSString stringWithFormat:@"%lu.txt", (unsigned long)counter+1];
NSString *path = [paramPath stringByAppendingPathComponent:fileName];
NSString *fileContents = [NSString stringWithFormat:@"Some text_%lu",(unsigned long)counter+1];
NSError *error = nil;
if ([fileContents writeToFile:path atomically:YES
encoding:NSUTF8StringEncoding error:&error] == NO){
NSLog(@"Failed to save file to %@. Error = %@", path, error);
}
}
NSLog(@"create files in folder succeed");
}
/* 遍历 */
- (void) enumerateFilesInFolder:(NSString *)paramPath{
NSError *error = nil;
NSArray *contents = [self.fileManager contentsOfDirectoryAtPath:paramPath error:&error];
if ([contents count] > 0 && error == nil){
NSLog(@"Contents of path %@ = \n%@", paramPath, contents);
}
else if ([contents count] == 0 && error == nil){
NSLog(@"Contents of path %@ is empty!", paramPath);
}
else {
NSLog(@"Failed to enumerate path %@. Error = %@", paramPath, error);
}
}
/* 删除 */
- (void) deleteFilesInFolder:(NSString *)paramPath{
NSError *error = nil;
NSArray *contents = [self.fileManager contentsOfDirectoryAtPath:paramPath error:&error];
if (error == nil){
for (NSString *fileName in contents){
/*filePath:the full path */
NSString *filePath = [paramPath stringByAppendingPathComponent:fileName];
if ([self.fileManager removeItemAtPath:filePath error:&error] == NO){
NSLog(@"Failed to remove item at path %@. Error = %@", fileName, error);
}
}
} else {
NSLog(@"Failed to enumerate path %@. Error = %@", paramPath, error);
}
}
/* 删除文件夹*/
- (void) deleteFolder:(NSString *)paramPath{
NSError *error = nil;
if ([self.fileManager removeItemAtPath:paramPath error:&error] == NO){
NSLog(@"Failed to remove path %@. Error = %@", paramPath, error);
}else {
NSLog(@"succeed in remove path %@",paramPath);
}
-(void) actionFilesAndFolders{
self.fileManager = [[NSFileManager alloc] init];
NSString *txtFolder = [NSTemporaryDirectory() stringByAppendingPathComponent:@"txt"];
[self createFolder:txtFolder];
[self createFilesInFolder:txtFolder];
[self enumerateFilesInFolder:txtFolder];
[self deleteFilesInFolder:txtFolder];
[self enumerateFilesInFolder:txtFolder];
[self deleteFolder:txtFolder];
}
- delete Files and Folders(删除文件、文件夹)
- 删除SVN文件 Delete SVN Folders.reg
- 删除SVN文件 Delete SVN Folders.reg
- enumerate Files and Folders(遍历)
- 删除SVN文件 Delete SVN Folders.reg
- dos xcopy files and folders
- Working with iPhone files and folders
- Rename files and folders with git
- java 删除文件夹下所有文件(java delete folder)
- Android: Loading files from the Assets and Raw folders
- Delete .svn folders on your Mac 删除 .svn
- Delete .svn folders on your Mac 删除 .svn
- Python 文件/文件夹操作(读写read/write、复制copy、删除delete、重命名rename)
- Python 文件/文件夹操作(读写read/write、复制copy、删除delete、重命名rename)
- javascript之操作文件夹(Folders)
- Delete SVN Folders
- Delete SVN Folders.reg
- 【Windows 7 – How to Delete Files Protected by TrustedInstaller Win7强制删除文件】
- 前端开发必备 40款优秀CSS代码编写工具推荐
- CCI 指标解释
- 算法导论10.2-8-用一个整数地址替代前后指针实现双向链表
- 计算机概念总结
- iOS UIWebView 与 JavaScript 交互 代码实现 嵌入 js 脚本
- delete Files and Folders(删除文件、文件夹)
- ZED-Board从入门到精通系列(六)——Vivado+OpenRISC
- 进程的创建: fork/execve
- uva 10205 模拟
- Jquery-zTree的基本用法
- Spring常用的接口和类
- #R#RWeka
- Spring Pool
- http://ioser.info/