rename a file in swift

swift, file handling, renaming files

let fileManager = FileManager.default let currentPath = "/path/to/current/file.txt" let newPath = "/path/to/new/file.txt"

do { try fileManager.moveItem(atPath: currentPath, toPath: newPath) } catch { print("Error while renaming file: (error)") }

main.swift

In the above code, `moveItem` method of `FileManager` class is used to rename a file. The `currentPath` variable contains the path of the file that needs to be renamed and `newPath` variable contains the path of the new file name that needs to be given to the file. 

The `try-catch` block is used to handle any errors that may occur during the renaming process.
364 chars
5 lines

related categories

gistlibby LogSnag