美文网首页
ios通过一个文件路径遍历目录下所有文件

ios通过一个文件路径遍历目录下所有文件

作者: 世玉茹花 | 来源:发表于2021-04-26 11:27 被阅读0次

递归思路:

- (void)showAllFileWithPath:(NSString *) path {
    NSFileManager * fileManger = [NSFileManager defaultManager];
    BOOL isDir = NO;
    BOOL isExist = [fileManger fileExistsAtPath:path isDirectory:&isDir];
    if (isExist) {
        if (isDir) {
            NSArray * dirArray = [fileManger contentsOfDirectoryAtPath:path error:nil];
            NSString * subPath = nil;
            for (NSString * str in dirArray) {
                subPath  = [path stringByAppendingPathComponent:str];
                BOOL issubDir = NO;
                [fileManger fileExistsAtPath:subPath isDirectory:&issubDir];
                [self showAllFileWithPath:subPath];
            }
        }else{
            NSString *fileName = [[path componentsSeparatedByString:@"/"] lastObject];
            if ([fileName hasSuffix:@".png"]) {
                //do anything you want
                [self.imageArray addObject:fileName];
            }else if([fileName hasSuffix:@".mv"]){
                    [self.imageArray addObject:fileName];
            }
        }
    }else{
        NSLog(@"this path is not exist!");
    }
}

相关文章

网友评论

      本文标题:ios通过一个文件路径遍历目录下所有文件

      本文链接:https://www.haomeiwen.com/subject/jwosrltx.html