I can copy files from my main bundle to my Documents directory by filename with the code below, but how can I copy based on wildcard? For example I have several files named myimageX.jpg (x representing a random number). I would like to loop through all of them that are contained in my main bundle and if they don’t exist in Documents, copy them.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
myfileName = @"myimage322.jpg";
documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
documentsDir = [documentPaths objectAtIndex:0];
filePath = [documentsDir stringByAppendingPathComponent:myfileName];
success = [fileManager fileExistsAtPath:filePath];
if(success) return;
NSString *filePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:myfileName];
[fileManager copyItemAtPath:filePathFromApp toPath:filePath error:nil];
[fileManager release];
The method fileExistsAtPath would be helpful. First enumerate through the contents of the bundle and check whether the same file (of the same filename) exists in the Documents.
Here is how to do it:
This is a modified version of the code from NSFileManager class reference that can be found here:
http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html