I think file.copy would be sufficient.
file.copy(from, to, overwrite = recursive, recursive = FALSE, copy.mode = TRUE)
From ?file.copy :
from, to: character vectors, containing file names or paths. For 'file.copy' and 'file.symlink' 'to' can alternatively be the path to a single existing directory.
and
recursive: logical. If 'to' is a directory, should directories in 'from' be copied (and their contents)? (Like 'cp -R' on POSIX OSes.)
From the description of recursive we know that from can have directories. Therefore, the above code lists all the files before copying. And just remember that the to directory would be the parent of the copied from . For example, after file.copy("dir_a/", "new_dir/", recursive = T) , new_dir will be dir_a .
Your code very much deleted the deletion part. unlink has a nice recursive parameter that file.remove does not support.
unlink(x, recursive = FALSE, force = FALSE)
source share