I think file.copy
shall 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 about recursive
we know from
can have directories. Therefore in your above code listing all files before copy is unnecessary. And just remember the to
directory would be the parent of the copied from
. For example, after file.copy("dir_a/", "new_dir/", recursive = T)
, there'd be a dir_a
under new_dir
.
Your code have done the deletion part pretty well. unlink
has a nice recursive
option, which file.remove
doesn't.
unlink(x, recursive = FALSE, force = FALSE)