首先查找到需要遍历文件夹的路径
//开始遍历
NSDirectoryEnumerator *dirEnumerater = [fm enumeratorAtPath:dir];
NSString *filePath = nil;
while (nil != (filePath = [dirEnumerater nextObject])) {
NSString *msgdir = [NSString stringWithFormat:@"%@/localFile/%@",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;
}
//文件名
NSString *fileNameStr = [filePath lastPathComponent];
//文件路径
NSString *filePath = msgdir;
//文件修改时间
NSDictionary *attributes = [fm attributesOfItemAtPath:msgdir error:nil];
NSDate *theModifiDate;
if ((theModifiDate = [attributes objectForKey:NSFileModificationDate])) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy.MM.dd HH:mm"];
NSString *dateStr = [formatter stringFromDate:theModifiDate];
NSFileManager* manager = [NSFileManager defaultManager];
//文件大小
long long fileSize;
if ([manager fileExistsAtPath:filePath]){
fileSize = [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
}
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:fileNameStr,@"name",filePath,@"path",dateStr,@"time",@(fileSize),@"size", nil];
[localFileArr addObject:dic];
NSLog(@"\n-----文件名%@\n_______文件路径%@\n======修改时间%@\n-=-=-=-大小 %lld",fileNameStr,filePath,dateStr,fileSize);
}
}
}
}