How to move file in Swift

October 15, 2017

To move file from your system from one location to another location you can use the following code

let home = FileManager.default.homeDirectoryForCurrentUser
let playgroundPath = "Documents/dev/xcode/DatePicker/c"

// get your playground URL
let playgroundURL = home.appendingPathComponent(playgroundPath)



// First create a FileManager instance
let fileManager = FileManager.default




let oldFile = playgroundURL.appendingPathComponent("test.txt").path

let newFile = playgroundURL.appendingPathComponent("test-new.txt").path
do{
    try fileManager.moveItem(atPath: oldFile, toPath: newFile)
} catch   {
    print("error")
}