1。对指定目录的浅遍历
- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error
e.g.
- (void) actionEnumerate{
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString *bundleDir = [[NSBundle mainBundle] bundlePath];
NSError *error = nil;
NSArray *bundleContents = [fileManager contentsOfDirectoryAtPath:bundleDir error:&error];
if ([bundleContents count] > 0 && error == nil){
NSLog(@"Contents of the app bundle = %@", bundleContents);
}
else if ([bundleContents count] == 0 && error == nil){
NSLog(@"Call the police! The app bundle is empty.");
}
else {
NSLog(@"An error happened = %@", error);
}
}
2。对指定目录的浅遍历(并能获取到结果中每一数据项的额外信息,如是否文件,创建时间等)
- (NSArray *)contentsOfDirectoryAtURL:(NSURL *)url includingPropertiesForKeys:(NSArray *)keys
options:(NSDirectoryEnumerationOptions)mask error:(NSError **)error
参数
includingPropertiesForKeys: NSURLIsDirectoryKey / NSURLIsReadableKey / NSURLCreationDateKey
NSURLContentAccessDateKey / NSURLContentModificationDateKey
options:0 //显示全部文件
NSDirectoryEnumerationSkipsHiddenFiles //忽略隐藏文件
3。获取文件属性 (NSURL类的实例方法)
- (BOOL)getResourceValue:(out id *)value forKey:(NSString *)propertyKey error:(out NSError **)error
e.g.
//获取指定文件夹(.app)下的内容(返回结果中的每一项都是NSURL,且带有各种属性,如创建日期,最后修改日期等)
- (NSArray *) contentsOfAppBundle{
NSFileManager *manager = [[NSFileManager alloc] init];
NSURL *bundleDir = [[NSBundle mainBundle] bundleURL];
//各种属性
NSArray *propertiesToGet = @[
NSURLIsDirectoryKey,
NSURLIsReadableKey,
NSURLCreationDateKey,
NSURLContentAccessDateKey,
NSURLContentModificationDateKey
];
NSError *error = nil;
NSArray *result = [manager contentsOfDirectoryAtURL:bundleDir includingPropertiesForKeys:propertiesToGet
options:0 error:&error];
if (error != nil){
NSLog(@"An error happened = %@", error);
}
return result;
}
- (NSString *) stringValueOfBoolProperty:(NSString *)paramProperty ofURL:(NSURL *)paramURL{
NSNumber *boolValue = nil;
NSError *error = nil;
[paramURL getResourceValue:&boolValue forKey:paramProperty error:&error];
if (error != nil){
NSLog(@"Failed to get property of URL. Error = %@", error);
}
return [boolValue isEqualToNumber:@YES] ? @"Yes" : @"No";
}
- (NSString *) isURLDirectory:(NSURL *)paramURL{
return [self stringValueOfBoolProperty:NSURLIsDirectoryKey ofURL:paramURL];
}
- (NSString *) isURLReadable:(NSURL *)paramURL{
return [self stringValueOfBoolProperty:NSURLIsReadableKey ofURL:paramURL];
}
- (NSDate *) dateOfType:(NSString *)paramType inURL:(NSURL *)paramURL{
NSDate *result = nil;
NSError *error = nil;
[paramURL getResourceValue:&result forKey:paramType error:&error];
if (error != nil){
NSLog(@"Failed to get property of URL. Error = %@", error);
}
return result;
}
- (void) printURLProperties:(NSURL *)paramURL{
NSLog(@"Item name = %@", [paramURL lastPathComponent]);
NSLog(@"Is a Directory? %@", [self isURLDirectory:paramURL]);
NSLog(@"Is Readable? %@", [self isURLReadable:paramURL]);
NSLog(@"Creation Date = %@",
[self dateOfType:NSURLCreationDateKey inURL:paramURL]);
NSLog(@"Access Date = %@",
[self dateOfType:NSURLContentAccessDateKey inURL:paramURL]);
NSLog(@"Modification Date = %@",
[self dateOfType:NSURLContentModificationDateKey inURL:paramURL]);
}
-(void)actionEnumerate {
NSArray *itemsInAppBundle = [self contentsOfAppBundle];
for (NSURL *item in itemsInAppBundle){
[self printURLProperties:item];
}
}
enumeratorAtURL:includingPropertiesForKeys:options:errorHandler: