iOS编程之NSFileManager

  • 在iOS编程过程中有时候需要对指定目录下的文件或者文件夹进行操作,那么就需要用到NSFileManager

  • 下面我们直接用实例来说明一下NSFileManager的具体用法

  • 场景如下:
    Snip20170111_21.png
  • 闲言少叙,直接上代码

NSFileManager *manager = [NSFileManager defaultManager];
NSError *error;
NSString *path = @"/Users/feng/Desktop/HTML5-视频整合";

//NSArray *contents = [manager contentsOfDirectoryAtPath:path error:&error];
NSArray *contents = [manager subpathsAtPath:path];
if (!error) {
    NSLog(@"正确查找文件内容!");
    //遍历内容打印所有内容名称
    [contents enumerateObjectsUsingBlock:^(NSString *content, NSUInteger idx, BOOL * _Nonnull stop) {
        //拼接成全路径
        NSString *fullPath = [path stringByAppendingPathComponent:content];
        //首先检查该内容是否是文件件
        BOOL isDirectory = NO;
        BOOL exists = [manager fileExistsAtPath:fullPath isDirectory:&isDirectory];
        if (exists && !isDirectory) {//文件
            NSString *fileName = [content lastPathComponent];
//                NSLog(@"%@---存在且是文件!", fileName);
            NSString *fileNametobeModified = [fileName stringByMatching:@"【.+】"];
            
            if(fileNametobeModified){
                NSLog(@"%@--文件需要修改",fileName);
                NSLog(@"%@----tobeModified--这是需要删除的字段", fileNametobeModified);
                
                NSString *ModifiedName = [fileName stringByReplacingOccurrencesOfString:fileNametobeModified withString:@""];
                
                NSLog(@"%@----修改完成的名字", ModifiedName);
                
                NSString *newFullPath = [fullPath stringByReplacingOccurrencesOfString:fileName withString:ModifiedName];
                NSLog(@"%@-----修改完成后全路径",newFullPath);
                
                [manager moveItemAtPath:fullPath toPath:newFullPath error:nil];
            }
            //删除指定名称的文件
            if ([fileName isEqualToString:@"课程使用必看【jkxy.taobao.com】.txt"]) {
                //NSLog(@"%@--------------可以删除", fullPath);
                NSError *error;
                [manager removeItemAtPath:fullPath error:&error];
                if (!error) {
                    NSLog(@"%@----文件删除成功!",fullPath);
                }
            }
        }else if (exists && isDirectory){//文件夹
            /*
            NSString *fileName = [content lastPathComponent];
            NSLog(@"%@---存在且是文件夹!", fileName);
            
            //删除指定名称的文件夹
            if ([fileName isEqualToString:@"极客学院"]) {
                NSLog(@"%@--------------可以删除", fullPath);
                NSError *error;
                [manager removeItemAtPath:fullPath error:&error];
                if (!error) {
                    NSLog(@"%@----文件删除成功!",fullPath);
                }
            }
             */
        }
        //NSLog(@"%@",content);
    }];
}else{
    NSLog(@"查找文件内容出错");
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 15,512评论 4 61
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,643评论 30 472
  • 《百年孤独》(1967年),是哥伦比亚作家加西亚·马尔克斯的代表作,也是拉丁美洲魔幻现实主义文学的代表作,被誉为“...
    尤汐Yogy阅读 10,116评论 85 256
  • 今天写代码出现了一个问题,从A页面push到B页面后,B页面的标题往右偏了,因为自定义的返回键,有点纳闷,后来查了...
    曹波波阅读 1,870评论 0 0
  • 第一次见清清是在实验室,她来找她的死党丹,丹彼时正跟着我在做毕业设计项目。清清随意跟我这个师兄打了个招呼,就开始和...
    于一潇阅读 832评论 2 3

友情链接更多精彩内容