开发者

Finding a filename with a wildcard in iPhone SDK

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 wo开发者_如何学编程uld 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.


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


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.

+(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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜