我已经根据某些条件创建了一个文件列表,我只想将该列表中的文件复制到新文件夹和子文件夹中,例如在原始文件夹中。文件夹的结构为年/月/日。
这是我尝试过的代码:
from.dir <- "J:/Radar_data/Beit_Dagan/RAW/2018"
## I want only the files from the night
to.dir <- "J:/Radar_data/Beit_Dagan/night"
files <- list.files(path = from.dir, full.names = TRUE, recursive =
TRUE)
## night_files is a vector I created with the files I need - only during the night
for (f in night_files) file.copy(from = f, to = to.dir)
但是我把所有文件都放在一个文件夹中
我的部分列表如下所示:
[1] "J:/Radar_data/Beit_Dagan/H5/2018/03/10/TLV180310142554.h5"
[2] "J:/Radar_data/Beit_Dagan/H5/2018/03/10/TLV180310142749.h5"
[3] "J:/Radar_data/Beit_Dagan/H5/2018/03/10/TLV180310143054.h5"
复制时有没有办法保留文件夹和子文件夹的结构?
我想在新的"夜"文件夹中获得相同的年/月/日结构
在复制调用中使用标志recursive = T
,因此您实际上不需要在 dir 内循环。
from = paste0(getwd(),"/output/","output_1")
to = paste0(getwd(),"/output/","output_1_copy")
file.copy(from, to, recursive = T)
请注意,您需要在调用之前创建/output_1_copy
目录。您可以手动或使用dir.create(...)
来完成。
你只需要:
file.copy(from = from.dir, to = to.dir,recursive=T)