I am trying to bulk move files of different kinds in R.
origindir <- c("c:/origindir")
targetdir <- c("c/targetdir")
filestocopy <- c("myfile.doc", "myfile.rda", "myfile.xls",
"myfile.txt", "myfile.pdf", "myfile.R")
I tried the following, but do not know how to do for all files:
file.copy(paste (origindir, "myfile.doc", sep = "/"),
paste (targetdir, "myfile.doc", sep = "/"),
overwrite = recursive, recursive = FALSE,
copy.mode = TRUE)
I do not know how to do this.
copy() function as shown in the following R syntax. Within the file. copy function, we have to specify the directory path and file names of the first folder from which we want to copy the files, as well as the directory path and file names of the second folder to which we want to copy the files.
To list all files in a directory in R programming language we use list. files(). This function produces a list containing the names of files in the named directory. It returns a character vector containing the names of the files in the specified directories.
To copy a file in R, use the file. copy() method. The file. copy() function works in the same way as a file.
As Joran and Chase have already pointed out in the comments, all you need to do is:
file.copy(from=filestocopy, to=targetdir,
overwrite = recursive, recursive = FALSE,
copy.mode = TRUE)
Then, if you're actually moving the files, remove the originals with:
file.remove(filestocopy)
Just expanding Chase's suggestion.
lapply(filestocopy, function(x) file.copy(paste (origindir, x , sep = "/"),
paste (targetdir,x, sep = "/"), recursive = FALSE, copy.mode = TRUE))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With