Moving files between folders

Use file.copy() or fs::file_copy()

file.copy(from = "path_to_original_file",
          to   = "path_to_move_to")

Then you can remove the original file with file.remove():

file.remove("path_to_original_file")

Update 2021-10-08: you can also use fs::file_copy(). I like {fs} for consistent file and directory management from within R.


If you wanted a file.rename()-like function that would also create any directories needed to carry out the rename, you could try something like this:

my.file.rename <- function(from, to) {
    todir <- dirname(to)
    if (!isTRUE(file.info(todir)$isdir)) dir.create(todir, recursive=TRUE)
    file.rename(from = from,  to = to)
}

my.file.rename(from = "C:/Users/msc2/Desktop/rabata.txt",
               to = "C:/Users/msc2/Desktop/Halwa/BADMASHI/SCOP/rabata.txt")

Please just be aware that file.rename will actually delete the file from the "from" folder. If you want to just make a duplicate copy and leave the original in its place, use file.copy instead.