如何将文件夹和子文件夹中的文件复制到R中的另一个文件夹中?

时间:2022-12-25 07:10:20

I would like to copy files only from 1 root folder that has 100's of folders and subfolders. I do not want to copy the folders. I just want to copy all the files (*.iso, *.txt, *.docx, *.pdf etc.) there are in these folders to another folder.

我只想从一个有100个文件夹和子文件夹的根文件夹复制文件。我不想复制文件夹。我只想复制所有的文件(*)。iso,*。txt,*。多克斯,*。这些文件夹里有另一个文件夹。

My code:

我的代码:

setwd("/Users/RLearner/Desktop/RDMS")

if (file.exists(list.files(path=".",recursive=TRUE)))
  file.copy(from=".", to="/Users/RLearner/Desktop/Test", recursive=TRUE)

But this code is copying the root folder as it is into my desired Test folder. I just want to copy the files that these folders have?

但是,这段代码正在复制根文件夹,因为它已经进入了我想要的测试文件夹。我只是想复制这些文件夹里的文件?

1 个解决方案

#1


3  

I would do:

我要做:

from.dir <- "/Users/RLearner/Desktop/RDMS"
to.dir   <- "/Users/RLearner/Desktop/Test"
files    <- list.files(path = from.dir, full.names = TRUE, recursive = TRUE)
for (f in files) file.copy(from = f, to = to.dir)

#1


3  

I would do:

我要做:

from.dir <- "/Users/RLearner/Desktop/RDMS"
to.dir   <- "/Users/RLearner/Desktop/Test"
files    <- list.files(path = from.dir, full.names = TRUE, recursive = TRUE)
for (f in files) file.copy(from = f, to = to.dir)