views:

112

answers:

1

In Cocoa, is there any way to copy all the files in a directory without copying the directory's subdirectories along with them?

+1  A: 

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) // ...
}
Georg Fritzsche
That works for me.
Nano8Blazex