sam*_*ich 134 iphone cocoa cocoa-touch objective-c ios
出于某些疯狂的原因,我无法找到一种方法来获取给定目录的带有glob的文件列表.
我目前仍然坚持以下几点:
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSArray *dirContents = [[NSFileManager defaultManager]
directoryContentsAtPath:bundleRoot];
Run Code Online (Sandbox Code Playgroud)
..然后剥掉我不想要的东西,这很糟糕.但我真正喜欢的是能够搜索"foo*.jpg"之类的东西,而不是要求整个目录,但我找不到那样的东西.
那你怎么做的呢?
Bri*_*ter 239
你可以在NSPredicate的帮助下轻松实现这一点,如下所示:
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *dirContents = [fm contentsOfDirectoryAtPath:bundleRoot error:nil];
NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.jpg'"];
NSArray *onlyJPGs = [dirContents filteredArrayUsingPredicate:fltr];
Run Code Online (Sandbox Code Playgroud)
如果您需要使用NSURL而不是它,它看起来像这样:
NSURL *bundleRoot = [[NSBundle mainBundle] bundleURL];
NSArray * dirContents =
[fm contentsOfDirectoryAtURL:bundleRoot
includingPropertiesForKeys:@[]
options:NSDirectoryEnumerationSkipsHiddenFiles
error:nil];
NSPredicate * fltr = [NSPredicate predicateWithFormat:@"pathExtension='jpg'"];
NSArray * onlyJPGs = [dirContents filteredArrayUsingPredicate:fltr];
Run Code Online (Sandbox Code Playgroud)
Mat*_*att 32
这非常适合IOS,但也应该适用cocoa.
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSFileManager *manager = [NSFileManager defaultManager];
NSDirectoryEnumerator *direnum = [manager enumeratorAtPath:bundleRoot];
NSString *filename;
while ((filename = [direnum nextObject] )) {
//change the suffix to what you are looking for
if ([filename hasSuffix:@".data"]) {
// Do work here
NSLog(@"Files in resource folder: %@", filename);
}
}
Run Code Online (Sandbox Code Playgroud)
Joh*_*ker 27
那么使用NSString的hasSuffix和hasPrefix方法呢?类似的东西(如果你正在搜索"foo*.jpg"):
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSArray *dirContents = [[NSFileManager defaultManager] directoryContentsAtPath:bundleRoot];
for (NSString *tString in dirContents) {
if ([tString hasPrefix:@"foo"] && [tString hasSuffix:@".jpg"]) {
// do stuff
}
}
Run Code Online (Sandbox Code Playgroud)
对于简单,直接的匹配,它比使用正则表达式库更简单.
Raj*_*han 12
非常简单的方法:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager contentsOfDirectoryAtPath:documentsDirectory
error:nil];
//--- Listing file by name sort
NSLog(@"\n File list %@",fileList);
//---- Sorting files by extension
NSArray *filePathsArray =
[[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory
error:nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF EndsWith '.png'"];
filePathsArray = [filePathsArray filteredArrayUsingPredicate:predicate];
NSLog(@"\n\n Sorted files by extension %@",filePathsArray);
Run Code Online (Sandbox Code Playgroud)
Bry*_*yle 10
Unix有一个可以为你执行文件通配操作的库.函数和类型在名为的头中声明glob.h,因此您需要#include它.如果打开一个终端,打开glob的手册页,键入man 3 glob你将获得使用这些函数你需要知道的所有信息.
下面是如何在数组中填充与globbing模式匹配的文件的示例.使用此glob功能时,您需要记住一些事项.
glob函数在当前工作目录中查找文件.为了搜索另一个目录,您需要将目录名称添加到globbing模式,就像我在我的示例中所做的那样,以获取所有文件/bin.glob通过调用来清理分配的内存globfree.在我的示例中,我使用默认选项,没有错误回调.手册页涵盖了所有选项,以防您想要使用其中的某些内容.如果您打算使用上面的代码,我建议将其添加为类别NSArray或类似内容.
NSMutableArray* files = [NSMutableArray array];
glob_t gt;
char* pattern = "/bin/*";
if (glob(pattern, 0, NULL, >) == 0) {
int i;
for (i=0; i<gt.gl_matchc; i++) {
[files addObject: [NSString stringWithCString: gt.gl_pathv[i]]];
}
}
globfree(>);
return [NSArray arrayWithArray: files];
Run Code Online (Sandbox Code Playgroud)
编辑:我在github上创建了一个要点,其中包含一个名为NSArray + Globbing的类别中的上述代码.
您需要使用自己的方法来消除不需要的文件.
使用内置工具并不容易,但您可以使用RegExKit Lite来帮助查找您感兴趣的返回数组中的元素.根据发行说明,这应该适用于Cocoa和Cocoa-Touch应用程序.
这是我在大约10分钟内编写的演示代码.我将<和>更改为"因为它们没有出现在pre块中,但它仍然适用于引号.也许在StackOverflow上有更多关于格式化代码的人会纠正这个(Chris?).
这是一个"基础工具"命令行实用程序模板项目.如果我在我的家庭服务器上启动并运行我的git守护程序,我将编辑此帖子以添加项目的URL.
#import "Foundation/Foundation.h"
#import "RegexKit/RegexKit.h"
@interface MTFileMatcher : NSObject
{
}
- (void)getFilesMatchingRegEx:(NSString*)inRegex forPath:(NSString*)inPath;
@end
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
MTFileMatcher* matcher = [[[MTFileMatcher alloc] init] autorelease];
[matcher getFilesMatchingRegEx:@"^.+\\.[Jj][Pp][Ee]?[Gg]$" forPath:[@"~/Pictures" stringByExpandingTildeInPath]];
[pool drain];
return 0;
}
@implementation MTFileMatcher
- (void)getFilesMatchingRegEx:(NSString*)inRegex forPath:(NSString*)inPath;
{
NSArray* filesAtPath = [[[NSFileManager defaultManager] directoryContentsAtPath:inPath] arrayByMatchingObjectsWithRegex:inRegex];
NSEnumerator* itr = [filesAtPath objectEnumerator];
NSString* obj;
while (obj = [itr nextObject])
{
NSLog(obj);
}
}
@end
| 归档时间: |
|
| 查看次数: |
104042 次 |
| 最近记录: |