开发者

Copy contents of Directories but excluding subdirectories in Cocoa

开发者 https://www.devze.com 2023-01-01 05:50 出处:网络
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 cond

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 NSFileManagers -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) // ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消