3
votes

I have a few items that are saved to the documents directory. I currently need those to be moved programmatically to another directory. I created the new directory successfully, but it doesn't seem to be looking at that when using FileManager.default.moveItem.

Code used to created directory.

let path = getDocumentsDirectory().appendingPathComponent("Media").path

        do{
            try FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: false, attributes: nil)

            print("\nDIRECTORY 'Media' CREATED")
        }catch {
            print("\nDIRECTORY 'Media' WAS NOT ABLE TO BE CREATED")
            print("ERROR - \(error)")
        } 

I use this code to check my URL.

let mediaURL = getDocumentsDirectory().appendingPathComponent("Media")
print("\nMEDIA URL: \(mediaURL)")

Which outputs this.

MEDIA URL:file:///Users/joseph/Library/Developer/CoreSimulator/Devices/552B72BF-8929-40EE-A75B-4574D3D2918A/data/Containers/Data/Application/E168B872-6A1D-4D78-B460-D45194088E5B/Documents/Media/

Here is the code I'm using to move the items.

do{
    try FileManager.default.moveItem(at: url, to: mediaURL)

    print("\nFILE MOVED TO NEW MEDIA PATH SUCCESSFULLY")
}catch {
    print("\nCOULDN'T MOVE FILE TO NEW MEDIA PATH")
    print("ERROR - \(error)")
}

Here is the error I'm receiving for each item that I try to move.

COULDN'T MOVE FILE TO NEW MEDIA PATH ERROR - Error Domain=NSCocoaErrorDomain Code=516 "“image_5” couldn’t be moved to “Documents” because an item with the same name already exists." UserInfo= {NSSourceFilePathErrorKey=/Users/joseph/Library/Developer/CoreSimulator/Devices/552B72BF-8929-40EE-A75B-4574D3D2918A/data/Containers/Data/Application/E168B872-6A1D-4D78-B460-D45194088E5B/Documents/image_5, NSUserStringVariant=( Move ), NSDestinationFilePath=/Users/joseph/Library/Developer/CoreSimulator/Devices/552B72BF-8929-40EE-A75B-4574D3D2918A/data/Containers/Data/Application/E168B872-6A1D-4D78-B460-D45194088E5B/Documents/Media, NSFilePath=/Users/joseph/Library/Developer/CoreSimulator/Devices/552B72BF-8929-40EE-A75B-4574D3D2918A/data/Containers/Data/Application/E168B872-6A1D-4D78-B460-D45194088E5B/Documents/image_5, NSUnderlyingError=0x60000024fff0 {Error Domain=NSPOSIXErrorDomain Code=17 "File exists"}}

The media directory should be empty, as it is newly created, so the error I'm receiving is a bit confusing. It's also saying it's trying to move it to 'Documents', but it should be moving to 'Media'.What is causing this issue, and how can I resolve it? I'm just trying to move the items saved in 'Documents' to the new directory, 'Media'.

3
You can try to append the file name to the destinationGIJOW

3 Answers

13
votes

You must append the file name to the destination URL. Per the docs:

The new location for the item in srcURL. The URL in this parameter must not be a file reference URL and must include the name of the file or directory in its new location. This parameter must not be nil.

2
votes

Delete the old file if exist

let url = NSUrl(string: "...")
if NSFileManager.defaultManager().fileExistsAtPath(url.path!) {
    try! NSFileManager.defaultManager().removeItemAtURL(url)
}

Update:

Swift 5.1.2

let url = URL(string: mp3FilePath)
if FileManager.default.fileExists(atPath: url!.path) {
    try! FileManager.default.removeItem(at: url!)
}
0
votes

This worked for me - Add the name of file too in destination path

let nameFile = (songPath as NSString).lastPathComponent
let fileDest = destinationDirectoryPath + "/" + nameFile
//Now move the song files
try fileManager.moveItem(atPath: sourceFilePath, toPath: fileDest)