iOS 遍历Document 文件夹 并获取文件属性

 
  
+ (NSMutableArray *)SeachAttachFileInDocumentDirctory{
    
    NSMutableArray *attachArray = [[NSMutableArray alloc]init];
    NSFileManager *fm = [NSFileManager defaultManager];
    //如果没有目录则创建信息存储目录
    [fm createDirectoryAtPath:[kDocumentsPath stringByAppendingPathComponent:@"MsgContent"]
                             withIntermediateDirectories:YES
                                              attributes:nil
                                                   error:nil];
    //递归枚举目录
    NSString *dir = [NSString stringWithFormat:@"%@/MsgContent",kDocumentsPath];
    NSDirectoryEnumerator *dirEnumerater = [fm enumeratorAtPath:dir];
    NSString *filePath = nil;
    while(nil != (filePath = [dirEnumerater nextObject])) {
        
        //NSLog(@"%@",filePath);
        NSString *msgdir = [NSString stringWithFormat:@"%@/MsgContent/%@",kDocumentsPath,filePath];
        BOOL isDir;
        if ([fm fileExistsAtPath:msgdir isDirectory:&isDir]) {
            if (!isDir){
                //读取不是目录得文件 将图片与附件遍历出来
                if([[filePath lastPathComponent] isEqualToString:@".DS_Store"])
                    continue;
                
                if([[filePath lastPathComponent] isEqualToString:@"index.htm"])
                    continue;
                
                if([[filePath lastPathComponent] isEqualToString:@"index.html"])
                    continue;
                
                AttachInfo *attachinfo = [[AttachInfo alloc]init];
                
                //获取文件名
                [attachinfo setAttachName:[filePath lastPathComponent]];
                //文件大小
                int size = getFileSizeFromPath((char *)[msgdir UTF8String]);
                [attachinfo setAttachSize:[NSString stringWithFormat:@"%d Bytes",size]];
                //文件全路径
                [attachinfo setAttachPath:msgdir];
                //文件类型
                [attachinfo setAttachtype:[filePath pathExtension]];
                //文件所属GUID
                NSArray *pathComponentsArr = [filePath pathComponents];
                [attachinfo setAttachLocalMsgGUID:[pathComponentsArr objectAtIndex:0]];
                //文件修改时间
                NSDictionary *attributes = [fm attributesOfItemAtPath:msgdir error:nil];
                NSDate *theModifiDate;
                if ((theModifiDate = [attributes objectForKey:NSFileModificationDate])) {
                    
                    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
                    [formatter setDateFormat:@"yyyy年M月d日 H点m分"];
                    [attachinfo setAttachModifyDate:[formatter stringFromDate:theModifiDate]];
                    [formatter release];
                }
                [attachArray addObject:attachinfo];
                [attachinfo release];
                
            }
        }
    }
//    for (AttachInfo *obj in attachArray) {
//        NSLog(@"attachLocalMsgGUID %@",[obj attachLocalMsgGUID]);
//        NSLog(@"attachName %@",[obj attachName]);
//        NSLog(@"attachtype  %@",[obj attachtype]);
//        NSLog(@"attachSize  %@",[obj attachSize]);
//        NSLog(@"attachPath  %@",[obj attachPath]);
//        NSLog(@"attachPath  %@",[obj attachModifyDate]);
//    }
    return [attachArray autorelease];
    
}

你可能感兴趣的:(Object-C)