16

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.

pnuts
  • 58,317
  • 11
  • 87
  • 139
jon
  • 11,186
  • 19
  • 80
  • 132
  • 1
    use `lapply` to iterate over your vector of `filestocopy`, create an anonymous function in your lapply call to replace everywhere you have `myfile.doc`...something like `lapply(filestocopy, function(x) file.copy(paste(origindir, x, sep = "/"), ...` – Chase Apr 24 '12 at 14:16
  • 6
    Also, look at `file.path()` as a replacement for `paste` above, it is supposed to be "faster than paste" and is used extensively within R itself. – Chase Apr 24 '12 at 14:17
  • 7
    @Chase I think the from and to arguments in `file.copy` are actually vectorized. – joran Apr 24 '12 at 14:18
  • @joran - cool! If that's the case, it should be as simple as replacing the `myfile.doc` with `filestocopy` then...right? – Chase Apr 24 '12 at 14:45
  • @Chase Yup. Just tried it out-from can be a vector of file paths and to can be a single directory path or vector of file paths. – joran Apr 24 '12 at 14:49
  • I didn't know it was vectorized. I've been using it inefficiently for some time now. Thanks Joran. – Tyler Rinker Apr 24 '12 at 15:29
  • When Chase's example with `lapply()` I get a list with `TRUE` statements in the console. Is it suppressable? – Toby Feb 19 '14 at 15:40

2 Answers2

18

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)
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • 3
    I got this message: `Error in file.copy(from = filestocopy, to = targetdir, overwrite = recursive, : object 'recursive' not found`. Isn't recursive a build in function? – Y. Z. Jul 07 '17 at 18:07
  • 1
    I had the same error. But the following code worked for me: `flist <- list.files(filestocopy, ".csv", full.names = TRUE); file.copy(flist, targetdir)` – Tanjil Jul 04 '20 at 23:12
  • You want `overwrite = TRUE` instead of `overwrite = recursive`. That'll solve the error. – ndem763 Sep 20 '22 at 18:06
1

Just expanding Chase's suggestion.

lapply(filestocopy, function(x) file.copy(paste (origindir, x , sep = "/"),  
          paste (targetdir,x, sep = "/"), recursive = FALSE,  copy.mode = TRUE))
SHRram
  • 4,127
  • 7
  • 35
  • 53
  • 3
    I don't know why you would use this when the default `file.copy` is able to handle a vector of file names. – A5C1D2H2I1M1N2O1R2T1 Apr 24 '12 at 19:15
  • NO. For some unknown reason. `Error in file.copy(from = TBL$FROM, to = TBL$TO[1]) : more 'from' files than 'to' files`' `TBL` is a tibble – Stat-R Jul 26 '21 at 06:25