enumerate Files and Folders(遍历)
来源:互联网 发布:实验二网络应用层协议 编辑:程序博客网 时间:2024/05/22 14:32
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:
- enumerate Files and Folders(遍历)
- dos xcopy files and folders
- delete Files and Folders(删除文件、文件夹)
- Working with iPhone files and folders
- Rename files and folders with git
- Android: Loading files from the Assets and Raw folders
- Windows 64-bit: The 'Program Files (x86)' and 'SysWOW64' folders explained
- Virtualbox and Shared Folders
- Virtualbox and Shared Folders
- enumerate 及python 遍历
- Display hidden files or folders with CutFTP
- Unlock files or folders using Unlocker
- About Folders, Directories, and Pathnames
- npm folders and dependencies resolve
- python中的list遍历--enumerate
- Enumerate and Change Display Modes
- Unity特殊文件及脚本编译顺序(Special folders and script compilation order)
- message file info and folders info
- Io 异常: Got minus one from a read call
- Eclispe中的ArcGIS Android SDK更新地址发生改变
- C# socket传输自定义对象
- 我在清华等你——桐华《最美的时光》读后感
- 查看与修改字节码,从而实现替换他人jar包中class文件的目的
- enumerate Files and Folders(遍历)
- k-svd
- java多线程总结
- 舌苔发白的原因
- crontab+shell实现java文件定时运行
- 通过 JavaScript 获取页面上的鼠标位置
- Android Design in Action — 以知乎为例
- js和jquery使按钮失效的方法
- 关于HASH分区