Beomseok Beomseok - 5 months ago 20
Objective-C Question

Copy folder from iPhone Resources directory to document directory

BOOL success;
NSFileManager *fileManager = [[NSFileManager defaultManager]autorelease];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:@"DB"];
success = [fileManager fileExistsAtPath:documentDBFolderPath];

if (success){
return;
}else{
NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"DB"];
[fileManager createDirectoryAtPath: documentDBFolderPath attributes:nil];
[fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath
error:&error];
}
}


Like this.

Resources/DB/words.csv => DB folder copy => Document/DB/words.csv

I want to copy DB subdirectory at Resources folder. I thought that source is good. But that source makes folder and doesn't copy files in DB folder at Resources folder.

I really want to copy files in DB folder at Resources folder. please help me.

Answer Source

1) Do not -autorelease the NSFileManager. You are double-releasing it which will crash your app.

2) No need to call -createDirectoryAtPath:. From the SDK doc of -copyItemAtPath:toPath:error:,

The file specified in srcPath must exist, while dstPath must not exist prior to the operation

and creating the directory the copy to fail.