Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将文件从文件夹和子文件夹复制到另一个文件夹,并保存文件夹的结构_R_File - Fatal编程技术网

R 将文件从文件夹和子文件夹复制到另一个文件夹,并保存文件夹的结构

R 将文件从文件夹和子文件夹复制到另一个文件夹,并保存文件夹的结构,r,file,R,File,我已经根据某些条件创建了一个文件列表,我只想将该列表中的文件复制到新文件夹和子文件夹中,如原始文件夹中的文件。 文件夹的结构为年/月/日 这是我尝试的代码: 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 <- "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)
复制时是否有办法保持文件夹和子文件夹的结构?
我想在新的“夜”文件夹中获得相同的年/月/日结构,您只需:

file.copy(from = from.dir, to = to.dir,recursive=T)

您需要在复制调用中使用标志
recursive=T
,因此实际上不需要在目录中循环

from=paste0(getwd(),“/output/”,“output_1”)
to=paste0(getwd(),“/output/”,“output\u 1\u copy”)
file.copy(from,to,recursive=T)

请注意,您需要在调用之前创建
/output\u 1\u copy
目录。您可以手动或使用
dir.create(…)

file.copy(from = from.dir, to = to.dir,recursive=T)