递归列出给定目录下的所有文件和目录
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了递归列出给定目录下的所有文件和目录相关的知识,希望对你有一定的参考价值。
/* This is example code of how to walk a directory recurisively and create a flat list of fully qualified names for all the files and directories under the supplied virtual root directory. */ #import <CoreServices/CoreServices.h> #import <AppKit/AppKit.h> #import <stdarg.h> int main (int argc, const char * argv[]) { int result = EXIT_SUCCESS; BOOL isDir; if (dir && ([fm fileExistsAtPath:dir isDirectory:&isDir] && isDir)) { if (![dir hasSuffix:@"/"]) { dir = [dir stringByAppendingString:@"/"]; } // this walks the |dir| recurisively and adds the paths to the |contents| set NSString *f; NSString *fqn; while ((f = [de nextObject])) { // make the filename |f| a fully qualifed filename fqn = [dir stringByAppendingString:f]; if ([fm fileExistsAtPath:fqn isDirectory:&isDir] && isDir) { // append a / to the end of all directory entries fqn = [fqn stringByAppendingString:@"/"]; } [contents addObject:fqn]; } NSString *fn; // here we sort the |contents| before we display them for ( fn in [[contents allObjects] sortedArrayUsingSelector:@selector(compare:)] ) { } } else { result = EXIT_FAILURE; } [pool release]; return result; }
以上是关于递归列出给定目录下的所有文件和目录的主要内容,如果未能解决你的问题,请参考以下文章