Copying a subset of files (names contained in a vector) from one folder to another folder in R

51 Views Asked by At

Ok, so I know similar questions were asked in the past, I checked them all and tried every bit of code I found and I am desperate

Here is my problem:

I have 2 folders, let's call them "A" and "B". Both are directly on my desktop. B is an empty folder that I want to fill with files from "A", but not all the files. "A" contains a total of 6000 files, and I only need 5000 specific ones to be copied to B. I don't need to delete or rename, really only copy.

The tocopy2 vector was created by taking a dataframe that had a column with all the names I need inside, then using list files I found them directly in my folder A, and then by a simple subset I stored them in the tocopy2 objects. I also tried taking directly the columns and storing it in a vector, but the results were the same

For now, I have a vector (class: character) that contains the specific full names of the 5000 files I need to copy. I also generated a second vector corrector containing their full paths in case it could come in handy. I have tried a looot of different variations of code using the file.copy() function, but it always end up returning FALSE for all the files or other funky errors.

tocopy is a vector containing all the files name paths is a vector containing all the paths

current= "C:/Users/pathtoA"
new= "C:/Users/pathtoB"

I'm sorry I have to censor the paths as i working with confidential data!

Edit following useful comments

Tocopy is a character vector containing file names, the file are .dcm files

Ex: tocopy= “file1.dcm” “file2.dcm” etc.

The paths for A and B are proper paths with enough / between elements ! I just can’t share their exact name as they are on a secure computer !

Thanks for the help

end of EDIT

Here is everything I tried so far

file.copy(from = paste0("C:/Users/pathtoA", tocopy), to = paste0("C:/Users/pathtoB", tocopy)) 
#returns only FALSE

file.copy(paths, to= "C:/Users/pathtoB") 
#Returns only false

file.copy(
  tocopy,
  from="C:/Users/pathtoA", 
  to="C:/Users/pathtoB", 
  recursive = TRUE, 
  overwrite= TRUE,
  copy.mode = TRUE, copy.dates = TRUE
) 
#Error in file.copy...unused argument (copy.dates = TRUE)

file.copy (tocopy2, from = current, to = new)
#Error in file.copy(tocopy, from = current, to = new) : invalid 'overwrite' argument

file.copy (tocopy2, from = current, to = new, overwrite = TRUE) 
#Error in recursive && to %in% from : invalid 'x' type in 'x && y'

If someone is able to find a solution I would be the most happy person and the most grateful!

Thanks in advance everyone

0

There are 0 best solutions below