Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying list of files from one folder to other in R

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.

like image 283
jon Avatar asked Apr 24 '12 14:04

jon


People also ask

How do I copy files from one directory to another in R?

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.

How do I get a list of files in a folder in R?

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.

How do I copy files in R studio?

To copy a file in R, use the file. copy() method. The file. copy() function works in the same way as a file.


2 Answers

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)
like image 95
A5C1D2H2I1M1N2O1R2T1 Avatar answered Oct 27 '22 00:10

A5C1D2H2I1M1N2O1R2T1


Just expanding Chase's suggestion.

lapply(filestocopy, function(x) file.copy(paste (origindir, x , sep = "/"),  
          paste (targetdir,x, sep = "/"), recursive = FALSE,  copy.mode = TRUE))
like image 33
SHRram Avatar answered Oct 26 '22 23:10

SHRram