在iPhone SDK中查找带有通配符的文件名
我正在尝试编写一个程序,该程序创建动态命名的 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通配符匹配
要使用通配符字符串进行匹配,查询可以使用 like 代替比较运算符,并包含“*”(匹配零个或多个字符)或“?” (精确匹配 1 个字符)作为通配符,如下所示:
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:
http://useyourloaf.com/blog/2010/7/27/filtering-arrays-with-nspredicate.html
看看 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.
其他发帖人或多或少所说内容的示例代码。
sample code for what the other poster said more or less.