Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS copy directories including subdirectories

I am working with iOS document folder called "temp" that allocates subdirectories that contain files dowloaded from remote url. Now I need to copy "temp" directory and all its contents to "main" folder (overwrite existing that was previously created). I know how to handle files but how to copy whole directory? Thank you

like image 514
Jaume Avatar asked Feb 02 '12 21:02

Jaume


2 Answers

You can use the same NSFileManager methods that you know and love for directories as well. For example:

if ([[NSFileManager defaultManager] fileExistsAtPath:pathToTempDirectoryInMain]) {
    [[NSFileManager defaultManager] removeItemAtPath:pathToTempDirectoryInMain error:nil];
}
NSError *copyError = nil;
if (![[NSFileManager defaultManager] copyItemAtPath:pathToTempDirectory toPath:pathToTempDirectoryInMain error:&copyError]) {
    NSLog(@"Error copying files: %@", [copyError localizedDescription]);
}
like image 104
jrtc27 Avatar answered Oct 18 '22 10:10

jrtc27


The NSFileManager methods removeItemAtPath:error:, removeItemAtURL:error:, copyItemAtPath:toPath:error:, and copyItemAtURL:toURL:error: methods handle directories.

You might also look at moveItemAtPath:toPath:error: and moveItemAtURL:toURL:error:.

like image 29
rob mayoff Avatar answered Oct 18 '22 11:10

rob mayoff