Swift: Failing to Copy Files to a Newly Created Folder

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

Failing to copy file from Bundle to Documents Directory Subfolder

You need to create a full path, including the filename.

Change:

let filePath =  tDocumentDirectory.appendingPathComponent("HTML")

to:

let filePath =  tDocumentDirectory.appendingPathComponent("HTML").appendingPathComponent(cssURL.lastPathComponent)

And the above use of lastPathComponent can be made possible by changing:

guard let cssPath = Bundle.main.path(forResource: "swiss", ofType: ".css") else {

to:

guard let cssURL = Bundle.main.url(forResource: "swiss", withExtension: "css") else {

And of course use cssURL.path in the call to copyItem.

Error when trying to copy file

This is possible when the destination path is missing the directory.
Make sure that the place at which you want to copy the source file is containing all required directory.

Error trying to copy from bundle to documents directory

Your code is almost correct. It has two flaws with this line:

NSString *destination = [[[Utils applicationDocumentsDirectory] absoluteString] stringByAppendingString:@"myapp.sqlite"];

The use of absoluteString is incorrect. This gives a file URL in the form file://path/to/Documents/. You need to use the path method to get a file path (not file URL) form the NSURL.

Then you need to properly append the filename to the path. You need to use stringByAppendingPathComponent: instead of stringByAppendingString:.

That line should be:

NSString *destination = [[[Utils applicationDocumentsDirectory] path] stringByAppendingPathComponent:@"myapp.sqlite"];

Unable to copy folder from application bundle to document directory

Photos seems like a group and not a folder reference. Hence all the images photo1.jpg.. etc are stored directly under your resource bundle.

The path <resource_bundle>/Photos does not exists. To verify this go to the application .app

.app->Right-click->Show Package Contents

Instead of adding Photos as a group, add it as Folder reference and under your target Copy Bundle Resources make sure the entire folder is added.

Hope that helps!

Swift - Move or update file fails

I figured out the issue, simply by sorting the code to required >only once tasks< and >tasks for each artifact< to take down pressure on the FileManager.

So this code handles the download and unarchiving and moving process for each artifact and it always checked the directories on a higher level if they exist and add them if they do not. For each artifact.
So this was already potential for a conflict when more than one download and unarchiving finishes almost at the same time and start doing the directory stuff.

Then, by preparing the folder for moving files made more trouble than it helped.
Although it works most of the time, it sometimes messes up the systems check to move the files and causes an error telling me that the directory or files already exist.

So before getting to each loadArtifact for the files, I reset needed directories and create higher level directories in order to prevent repeating checks in the loop.

Since that all works like expected.



Related Topics



Leave a reply



Submit