Search code examples
rfile-iofile-copying

Moving files between folders


I want to copy/paste a file from one folder to another folder in windows using R, but it's not working. My code:

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

[1] FALSE

Solution

  • 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")