在iPhone SDK中查找带有通配符的文件名

发布于 2024-10-19 19:40:07 字数 491 浏览 2 评论 0原文

我正在尝试编写一个程序,该程序创建动态命名的 .csv 文件,需要在以后的运行日期检索或删除这些文件。我想做的是:

我想运行一个算法来找出这些类型的文件是否存在。例如,如果我动态地将文件命名为 foobar##.csv 之类的文件,其中 ## 指示动态生成并附加到文件名的数字,我想查找是否存在任何 foobar##.csv 文件,无论使用的号码。通常我会使用这样的一行代码:

NSString *dataFileName = [[self documentPath] stringByAppendingPathComponent:@"foobar01.csv"];

现在我只是使用一个循环遍历每个值并在找到一个值时触发一个布尔值,但我知道这不是最佳实践,因为它限制了可能的文件名编号用户可以使用。任何关于如何在这样的搜索中使用某种通配符的见解将不胜感激。

另外,我想创建一种方法来删除程序找到的任何 .csv 文件,但我假设用于解决上述算法的方法也可以用于删除文件。

I am trying to write a program that creates dynamically named .csv files that need to be either retrieved or deleted at a later run date. What I am trying to do is this:

I would like to run an algorithm that will find out if any of these types of files exist. For example, if I dynamically name the file something like foobar##.csv with the ## indicating a number being dynamically being generated and appended to the filename, I would like to find if any foobar##.csv file exists regardless of the number used. Normally I would use a line of code like this:

NSString *dataFileName = [[self documentPath] stringByAppendingPathComponent:@"foobar01.csv"];

Right now I just use a loop that cycles through each value and trips a bool if one is found, but I know this isn't a best practice as it limits the possible filename numbers the user can use. Any insight into how I could use some sort of wildcard on a search like this would be appreciated.

Also, I would want to create a method that would delete any .csv files the program finds, but I'm assuming that the method used to solve the above algorithm can be used for the deletion one as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

厌味 2024-10-26 19:40:07

通配符匹配
要使用通配符字符串进行匹配,查询可以使用 like 代替比较运算符,并包含“*”(匹配零个或多个字符)或“?” (精确匹配 1 个字符)作为通配符,如下所示:

NSString *match = @"imagexyz*.png";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like %@", match];
NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];

http://useyourloaf.com /blog/2010/7/27/filtering-arrays-with-nspredicate.html

Wildcard match
To match using a wildcard string the query can use like instead of the comparison operator and include the “*” (match zero or more characters) or “?” (match exactly 1 character) as wildcards as follows:

NSString *match = @"imagexyz*.png";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like %@", match];
NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];

http://useyourloaf.com/blog/2010/7/27/filtering-arrays-with-nspredicate.html

以酷 2024-10-26 19:40:07

看看 NSFileManagers< /a> contentsOfDirectoryAtPath:error: 方法。它将返回一个带有字符串的数组,其中包含相关目录的所有对象(文件和目录)的名称。

然后,您可以枚举该数组并检查这些字符串中是否出现“foobar”。要么立即对找到的文件执行某些操作,要么将“正”文件名存储在另一个数组中以供以后处理。

Take a look at NSFileManagers contentsOfDirectoryAtPath:error: method. It will return an array with strings containing the names of all objects (files and directories) of the directory in question.

You could then enumerate through that array and check for occurances of "foobar" in those strings. Either do something to the files you found right away or store the "positive" filenames in another array for later processing.

药祭#氼 2024-10-26 19:40:07

其他发帖人或多或少所说内容的示例代码。

+(NSMutableArray*) allocLocalFiles
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSError * error = nil;
    NSArray *origContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory 
                                                                                error:&error];
    NSMutableArray * files = [[NSMutableArray alloc]init];

    for (NSString* file in origContents) {
        NSString * ext = [file pathExtension];
        if ([ext compare:@"csv"]==0 && something_else)
        {
            [files addObject:
             [NSString stringWithFormat:@"%@/%@",documentsDirectory,file]];
        }
    }
    return files;
}

sample code for what the other poster said more or less.

+(NSMutableArray*) allocLocalFiles
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSError * error = nil;
    NSArray *origContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory 
                                                                                error:&error];
    NSMutableArray * files = [[NSMutableArray alloc]init];

    for (NSString* file in origContents) {
        NSString * ext = [file pathExtension];
        if ([ext compare:@"csv"]==0 && something_else)
        {
            [files addObject:
             [NSString stringWithFormat:@"%@/%@",documentsDirectory,file]];
        }
    }
    return files;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文