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
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:©Error]) {
NSLog(@"Error copying files: %@", [copyError localizedDescription]);
}
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:
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With