Swift: failing to copy files to a newly created folder

From the copyItemAtPath(...) documentation:

dstPath
The path at which to place the copy of srcPath. This path must
include the name of the file or directory in its new location. …

You have to append the file name to the destination directory for the
copyItemAtPath() call (code updated for Swift 3 and later)

let srcURL = URL(fileURLWithPath: fullElementPath)
let destURL = URL(fileURLWithPath: newMTSFolder).appendingPathComponent(srcURL.lastPathComponent)

do {
    try FileManager.default.copyItem(at: srcURL, to: destURL)
} catch {
    print("copy failed:", error.localizedDescription)
}

Leave a Comment