Move the images or files from one folder to document directory iOS swift

Solution:

The below solution is used for move the entire folder data to the document directory.
You can move the entire folder data except text files from that folder is like below.


    // #MARK: - move the zip folder files to document directory
    
    func movefilestoDocument(folderPath:String){
        let documentsPath = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])
        let path = URL(fileURLWithPath: folderPath)
        
        do {
            // Get the directory contents urls (including subfolders urls)
            let directoryContents = try FileManager.default.contentsOfDirectory(at: path, includingPropertiesForKeys: nil)
            print(directoryContents)
            // if you want to filter the directory contents you can do like this:
            let directoryFiles = directoryContents.filter{ $0.pathExtension != "txt" }
            for i in 0..<directoryFiles.count {
                let srcURL = directoryFiles[i]
                let destURL = URL(fileURLWithPath: documentsPath.path).appendingPathComponent(srcURL.lastPathComponent)
                do {
                    try FileManager.default.copyItem(at: srcURL, to: destURL)
                } catch {
                    print("copy failed:", error.localizedDescription)
                }
            }
            
        } catch {
            print(error)
        }
        
        
    }

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift