Copy list of files to list of folders one-to-one in R

Viewed 51

In R, I need to copy the files listed in a vector into the folders listed in another vector in a one-to-one correspondence.

In the example below, I want sc1.png to be copied into the test1 folder and sc2.png into the test2 folder.

I tried this, but it didn't work:

files <- c("~/Desktop/test/sc1.png", "~/Desktop/test/sc2.png")
dirs <- c("~/Desktop/test/test1", "~/Desktop/test/test2")

file.copy(files, dirs)

I'd appreciate any help. Thanks.

1 Answers

Attach the filenames to dirs, you can use basename to get the filename.

file.copy(files, paste0(dirs, '/', basename(files)))
Related