2

Here is my Folder Structure

enter image description here

I want to copy this Stickers folder to Document Directory for this purpose i have used following code

    let fileManager = FileManager.default

    let documentsUrl = fileManager.urls(for: .documentDirectory,
                                        in: .userDomainMask)

    guard documentsUrl.count != 0 else {
        return // Could not find documents URL
    }

    let finalDatabaseURL = documentsUrl.first!.appendingPathComponent("Stickers")

    if !( (try? finalDatabaseURL.checkResourceIsReachable()) ?? false) {
        print("DB does not exist in documents folder")

        let documentsURL = Bundle.main.resourceURL?.appendingPathComponent("Stickers")

        do {
            try fileManager.copyItem(atPath: (documentsURL?.path)!, toPath: finalDatabaseURL.path)
        } catch let error as NSError {
            print("Couldn't copy file to final location! Error:\(error.description)")
        }

    } else {
        print("Database file found at path: \(finalDatabaseURL.path)")
    }

but it won't work. I am getting an Error as

enter image description here

NOTE: If Copying folder is not possible then i want to copy Assets.xcassets to Document Directory. How can i achieve it?

Khushbu Desai
  • 1,003
  • 1
  • 10
  • 32

1 Answers1

6

Please below code.. I update your code in two functions to copy all files from folder to document directory.

Hope it will work.

func copyFolders() {
    let fileManager = FileManager.default

    let documentsUrl = fileManager.urls(for: .documentDirectory,
                                        in: .userDomainMask)

    guard documentsUrl.count != 0 else {
        return // Could not find documents URL
    }

    let finalDatabaseURL = documentsUrl.first!.appendingPathComponent("Stickers")

    if !( (try? finalDatabaseURL.checkResourceIsReachable()) ?? false) {
        print("DB does not exist in documents folder")

        let documentsURL = Bundle.main.resourceURL?.appendingPathComponent("Stickers")

        do {
            if !FileManager.default.fileExists(atPath:(finalDatabaseURL?.path)!)
            {
                try FileManager.default.createDirectory(atPath: (finalDatabaseURL.path), withIntermediateDirectories: false, attributes: nil)
            }
            copyFiles(pathFromBundle: (documentsURL?.path)!, pathDestDocs: finalDatabaseURL.path)
        } catch let error as NSError {
            print("Couldn't copy file to final location! Error:\(error.description)")
        }

    } else {
        print("Database file found at path: \(finalDatabaseURL.path)")
    }

}

func copyFiles(pathFromBundle : String, pathDestDocs: String) {
    let fileManagerIs = FileManager.default
    do {
        let filelist = try fileManagerIs.contentsOfDirectory(atPath: pathFromBundle)
        try? fileManagerIs.copyItem(atPath: pathFromBundle, toPath: pathDestDocs)

        for filename in filelist {
            try? fileManagerIs.copyItem(atPath: "\(pathFromBundle)/\(filename)", toPath: "\(pathDestDocs)/\(filename)")
        }
    } catch {
        print("\nError\n")
    }
}
Community
  • 1
  • 1
Rakesh Patel
  • 1,673
  • 10
  • 27
  • Hey, My folder is copied in Document folder but i want data also which is inside my folder can you please help – Khushbu Desai Jun 21 '18 at 08:19
  • This code creates Folder in Document Directory but it did not copy my Stickers in this folder – Khushbu Desai Jun 21 '18 at 10:02
  • you should pick a file from this folder. Don't copy folder and at directory side must give a file name where you copy. – Rakesh Patel Jun 21 '18 at 11:25
  • I update a code...check and use it. just call ---> copyFolders() – Rakesh Patel Jun 21 '18 at 13:01
  • Sorry i was checked bundle path in -->if !FileManager.default.fileExists(atPath:(finalDatabaseURL?.path)!). I updated it...try now. hope it will work – Rakesh Patel Jun 22 '18 at 06:17
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/173598/discussion-between-rakesh-patel-and-khushbu-desai). – Rakesh Patel Jun 22 '18 at 06:27
  • @KhushbuDesai : Please check this out. I also had the same problem of subfolders and files not getting copied to the destination folder. https://stackoverflow.com/q/9830061/3223088 – Vin Sep 09 '20 at 14:03