I am trying to copy a folder in my NSBundle which contains quite a number of images.
I tried doing it with these codes.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:@"Images"];
NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"Images"];
[fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error];
Lets say there's an image in the folder named Test.png and i want to display the image on my button, it does not work!
However, if i only copied a single image from the NSBundle to NSDocumentDirectory, it works!
Example:
Changing
stringByAppendingPathComponent:@"Images"
To
stringByAppendingPathComponent:@"Test.png"
So the problem lies with copying the folder to NSDocumentDirectory!
Are my codes above incorrect?
Or it is impossible to copy folders? (Which means i have to copy files individually)
Oscar is correct, there is currently no way to copy an entire folder with a single command.
A method such as this might do the trick (forewarning - my office connection is down and I can't access my Mac to verify this code is working. Will verify this once I am able). Simply call with [self copyDirectory:@"Images"] and it will handle the rest.
-(void) copyDirectory:(NSString *)directory {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:directory];
NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:directory];
if (![fileManager fileExistsAtPath:documentDBFolderPath]) {
//Create Directory!
[fileManager createDirectoryAtPath:documentDBFolderPath withIntermediateDirectories:NO attributes:nil error:&error];
} else {
NSLog(@"Directory exists! %@", documentDBFolderPath);
}
NSArray *fileList = [fileManager contentsOfDirectoryAtPath:resourceDBFolderPath error:&error];
for (NSString *s in fileList) {
NSString *newFilePath = [documentDBFolderPath stringByAppendingPathComponent:s];
NSString *oldFilePath = [resourceDBFolderPath stringByAppendingPathComponent:s];
if (![fileManager fileExistsAtPath:newFilePath]) {
//File does not exist, copy it
[fileManager copyItemAtPath:oldFilePath toPath:newFilePath error:&error];
} else {
NSLog(@"File exists: %@", newFilePath);
}
}
}
-- EDIT 11/9/2011 -- Sorry about that, as I forewarned I was not able to access my mac to verify the code. Updated the code and I have verified it is working properly now. Added a couple quick NSLogs to tell you if the new directory or the new file already exists.