简体   繁体   中英

Swift: Copy folder/directory without files

I want to copy a folder without the files inside to a new location. If I create it newly simply by name, I would miss all attribues. Is there a function to copy it (including access rights, color tags, etc...)?

You can set the attributes for the clones when you create them:

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)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM