Created
May 15, 2015 08:58
-
-
Save kaishin-r/778559fad139e3e6372d to your computer and use it in GitHub Desktop.
Function of merge directory A to directory B
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)mergeContentsOfPath:(NSString *)srcDir intoPath:(NSString *)dstDir withError:(NSError **)error { | |
NSLog(@"Merge from Dir: %@ to Dir: %@", srcDir, dstDir); | |
NSString *sourceBasePath = srcDir; | |
NSString *destBasePath = dstDir; | |
NSURL *destBaseURL = [NSURL fileURLWithPath:destBasePath]; | |
int subPathIndex = (int)[sourceBasePath length] + 1; | |
NSFileManager *fileManager = [NSFileManager defaultManager]; | |
NSDirectoryEnumerator *dirEnum = [fileManager enumeratorAtURL:[NSURL fileURLWithPath:sourceBasePath] | |
includingPropertiesForKeys:[NSArray arrayWithObject:NSURLIsDirectoryKey] | |
options:NSDirectoryEnumerationSkipsHiddenFiles | |
errorHandler:nil]; | |
for (NSURL *url in dirEnum) { | |
NSString *subPath = [[url path] substringFromIndex:subPathIndex]; | |
NSNumber *isDir; | |
[url getResourceValue:&isDir forKey:NSURLIsDirectoryKey error:NULL]; | |
if ([isDir boolValue]) { | |
[fileManager createDirectoryAtPath:[destBasePath stringByAppendingPathComponent:subPath] withIntermediateDirectories:YES attributes:nil error:nil]; | |
} else { | |
if ([fileManager fileExistsAtPath:[destBaseURL URLByAppendingPathComponent:subPath].path]) { | |
[fileManager removeItemAtURL:[destBaseURL URLByAppendingPathComponent:subPath] error:nil]; | |
[fileManager copyItemAtURL:url toURL:[destBaseURL URLByAppendingPathComponent:subPath] error:nil]; | |
} else { | |
[fileManager copyItemAtURL:url toURL:[destBaseURL URLByAppendingPathComponent:subPath] error:nil]; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment