简体   繁体   English

iOS / Swift 4:如何在不删除文件和文件夹现有目标的情况下复制包含多个包含文件的文件夹的文件夹?

[英]iOS/Swift 4: How to copy a folder containing several folders containing files without removing the files and folders existing destination?

I would like to copy a folder containing several folders, and this folders contain several files. 我想复制一个包含几个文件夹的文件夹,并且该文件夹包含几个文件。 If this folders are existing, I would like to keep them inside, and overriding the files if existing. 如果此文件夹存在,我想将它们保留在其中,并覆盖文件(如果存在)。 If the files doesn't exist, they are simply naturally added. 如果文件不存在,则自然地添加它们。

_ = try FileManager.default.replaceItemAt(previousItemUrl, withItemAt: currentItemUrl)

For example: 例如:

Files existing on device: 设备上现有的文件:

APP/XXX/a.txt
APP/XXX/b.txt
APP/XXX/c.txt
APP/YYY/d.txt
APP/ZZZ/e.txt
APP/ZZZ/f.txt

Files to copy: 要复制的文件:

APP/XXX/c.txt //Will override
APP/YYY/g.txt //new file added
APP/ZZZ/h.txt //new file added
APP/AAA       //new folder added

Do I have to create my self a recursive function, or did that function already exists natively? 我必须创建自己的递归函数,还是该函数本身已经存在?

Thanks in advance. 提前致谢。

We can use below native method. 我们可以使用下面的本机方法。 It copies the source to destination. 它将源复制到目标。

func copyItem(atPath srcPath: String, 
   toPath dstPath: String) throws

We can also control the override use case by implementing other delegate methods mentioned below. 我们还可以通过实现下面提到的其他委托方法来控制替代用例。

optional func fileManager(_ fileManager: FileManager, 
         shouldCopyItemAt srcURL: URL, 
                       to dstURL: URL) -> Bool

You can find all these details in Apple document 您可以在Apple文档中找到所有这些详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在iOS中以编程方式从文档文件夹重命名文件/文件夹 - How to rename files/folders from document folder programmatically in iOS 如何在iOS中设置文件夹/文件的权限 - How to Set Permission for folders/files in iOS 如何在 iOS 上启用文件和文件夹权限 - How to enable files and folders permission on iOS 如何在iOS捆绑包的文件夹层次结构中包括所有文件,但不包括文件夹本身? - How do I include all files within a folder hierarchy in an iOS bundle, but not include the folders themselves? 如何获取iPhone资源文件夹中的文件夹和文件列表? - How get list of folders and files from resource folder in iPhone? 如何为文件夹中的所有文件和文件夹设置NSURL资源值 - How to set NSURL Resource Values for all files and folders within a folder 使用NSFileManager合并文件夹,仅覆盖现有文件 - Merge folders with NSFileManager, overwrite only existing files 以编程方式访问Assets文件夹中用于TVOS的文件/文件夹 - Access the files/folders in the Assets folder for TVOS programmatically iOS-Access App创建的文件和文件夹 - iOS - Access App created Files and Folders app扩展如何访问包含app Documents /文件夹的文件 - How can app extension access files in containing app Documents/ folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM