objective c - 将文件复制到现有目录

objective c - copy files into existing directory

如何在不复制整个目录的情况下将文件从 DIR1 复制到 DIR2?

我正在尝试将文件复制到包含其他文件的文件夹,因此我不想创建新文件夹。以下代码的作用:

[[NSFileManager defaultManager] copyItemAtPath:sourceDir toPath:destDir error:&err];

获取文件夹中的所有内容

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSArray *fileList = [[NSFileManager defaultManager] directoryContentsAtPath:documentsDirectory];

枚举文件列表并复制它们。

[fileList enumerateObjectsUsingBlock:^(NSString *string, NSUInteger index, BOOL *stop) {
   [[NSFileManager defaultManager]copyItemAtPath:strdestination toPath:toPath error:&Error];
}];

我是这样做的:

NSArray *sourceFiles = [fileManager contentsOfDirectoryAtPath:sourceDir error:NULL];

NSError *copyError = nil;

for (NSString *currentFile in sourceFiles)
{
    BOOL isDirectory = NO;
    NSString* fullFilePath = [NSString stringWithFormat:@"%@/%@",sourceDir,currentFile];
    if ([fileManager fileExistsAtPath:fullFilePath isDirectory:&isDirectory] && !isDirectory)
    {
        if (![fileManager copyItemAtPath:[sourceDir stringByAppendingPathComponent:currentFile] toPath:[destDir stringByAppendingPathComponent:currentFile] error:&copyError])
        {
            NSLog(@"Error Copying: %@", [copyError description]);
        }
    }
}