Copy contents of Directories but excluding subdirectories in Cocoa
In Cocoa, is there any way to copy all the files in a directory without copying the directory's sub开发者_如何学Pythondirectories along with them?
One way would be to copy items in the directory conditionally based on the results of NSFileManager
s -fileExistsAtPath:isDirectory:
:
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *files = [manager contentsOfDirectoryAtPath:pathFrom error:nil];
for (NSString *file in files) {
NSString *fileFrom = [pathFrom stringByAppendingPathComponent:file];
BOOL isDir;
if (![manager fileExistsAtPath:fileFrom isDirectory:&isDir] || isDir) {
continue;
}
NSString *fileTo = [pathTo stringByAppendingPathComponent:file];
NSError *error = nil;
[manager copyItemAtPath:fileFrom toPath:fileTo error:&error];
if (error) // ...
}
精彩评论