Swift:复制没有文件的文件夹/目录

时间:2016-09-01 11:27:48

标签: swift macos copy directory

我想将没有文件的文件夹复制到新位置。如果我仅仅通过名字新创建它,我会错过所有的归因。是否有复制它的功能(包括访问权限,颜色标签等)?

1 个答案:

答案 0 :(得分:2)

您可以在创建克隆时设置克隆的属性:

let targetPath = "..." as NSString
let destinationPath = "..." as NSString

let fileManager = NSFileManager.defaultManager()
let enumerator = fileManager.enumeratorAtPath(targetPath as String)!

for item in enumerator {
    let fullTargetPath = targetPath.stringByAppendingPathComponent(item as! String)
    let attributes = try! fileManager.attributesOfItemAtPath(fullTargetPath)

    if let fileType = attributes[NSFileType] as? String where fileType == NSFileTypeDirectory {
        let fullDestinationPath = destinationPath.stringByAppendingPathComponent(item as! String)
        try! fileManager.createDirectoryAtPath(fullDestinationPath, withIntermediateDirectories: true, attributes: attributes)
    }
}