How to move file in Swift

Last Updated: March 21, 2022

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
    try fileManager.moveItem(atPath: oldFile, toPath: newFile)
} catch   {