'Copying files where destination folder does not exist
I am writing an application to do a bulk import of files. I have got most of it working, but the process grinds to a halt when the destination folder doesn’t exist.
Here is a simplified version of the code:
let fm = FileManager.default
do {
try fm.copyItem(atPath: "/…/source/file.ext",toPath: "/…/destination/NewFolder/file.ext")
}
catch let error as NSError {
print("Drat: \(error)")
}
In this case, NewFolder needs to be created. If I don’t, I get a message something like:
Drat: Error Domain=NSCocoaErrorDomain Code=4 "The file “file.ext” doesn’t exist." UserInfo={NSSourceFilePathErrorKey=/…/source/file.ext, NSUserStringVariant=( Copy ), NSDestinationFilePath=/…/destination/NewFolder/file.ext, NSFilePath=/…source/file.ext, NSUnderlyingError=0x7fbaa9071270 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
I think this means that the destination folder doesn’t exist, though it’s not at all obvious from the message. I find that if I remove the NewFolder/ part of the destination, the file is copied successfully.
How can I get swift to create the missing destination folder.
I might add that being a bulk copy, there will be multiple missing destination folders to be created.
Solution 1:[1]
Perhaps this is too complex or too much code, but have you tried it this way?
/// This method copies the objects in the pathList from the given sourceURL to the targetURL. If the object in the pathList is a folder the existence is being checked by calling
/// checkExistenceOf(path: String, at sourcePath: String, in targetPath: String). If the object is not a directory, the object is copied from the source to the target. The delegate is expected to
/// handle the conflict options set by the user.
/// - Parameters:
/// - sourceURL: The source folder from where the objects are being copied.
/// - targetURL: The target folder to where the objects need to be copied.
/// - pathList: The objects that need to be copied.
private func copy(sourceURL: URL, to targetURL: URL, with pathList: [String]) {
for path in pathList {
let fullSourceURL = sourceURL.appendingPathComponent(path)
let reportPath = path
let reportSource = fullSourceURL.lastPathComponent.replacingOccurrences(of: "%20", with: " ")
let reportTarget = targetURL.path
if fullSourceURL.hasDirectoryPath {
//The path is a directory and we are checking the existence.
checkExistenceOf(path: path, at: sourceURL, in: targetURL)
} else {
//The path is a file and we are going to copy it.
let fullTargetURL = targetURL.appendingPathComponent(path)
do {
try mainFileManager.manager.copyItem(at: fullSourceURL, to: fullTargetURL)
} catch {
print(error.localizedDescription)
}
}
}
}
Then with the method checkExistenceOf() like this:
/// This method checks if a folder in the source location already exists in the target location. If the folder does not exist at the target, the folder is created by the method.
/// - Parameters:
/// - path: The folder that needs to be checked.
/// - sourcePath: The source location where the path already exists.
/// - targetPath: The target location where the existence of path is being chekced.
private func checkExistenceOf(path: String, at sourceURL: URL, in targetURL: URL) {
//The function fileExists can't deal with the %20 for space and the file:// for the source and target.
let sourcePath = sourceURL.relativePath
let targetPath = targetURL.relativePath
let fullSourcePath = String(sourcePath + "/" + path)
let fullTargetPath = String(targetPath + "/" + path)
//If the path doesn't exist create the folder at target with the same attributes as pth at source.
if !mainFileManager.manager.fileExists(atPath: fullTargetPath) {
let newDirectory = targetURL.appendingPathComponent(path)
do {
let attributes = try mainFileManager.manager.attributesOfItem(atPath: fullSourcePath)
try mainFileManager.manager.createDirectory(at: newDirectory, withIntermediateDirectories: false, attributes: attributes)
} catch {
print(error.localizedDescription)
}
}
}
The code comes from a previous project. My apologies for not scrubbing it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | halfer |
