Peter71 Peter71 - 1 year ago 247
Swift Question

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...)?

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download