我知道之前有人问过这个问题,但与仅使用 file.rename() 函数相比有细微的变化。
我创建了一个变量,它使用 setdiff 函数来比较文件夹 1 有哪些文件而文件夹 2 没有。根据文件名,文件夹 1 中大约有 100 个文件是文件夹 2 没有的。我想将这 100 个文件移动到文件夹 3。
我该怎么做呢?
我会使用 if then 语句吗?
最佳答案
假设您有一个要复制的名称列表并且目标文件夹已经存在:
# vector with the 100 files names to be copied
names <- c("text1.txt", "text2.txt")
# custom function
my_function <- function(x){
file.rename( from = file.path("yourpath/folder1", x) ,
to = file.path("yourpath/folder3", x) )
}
# apply the function to all files
lapply(names, my_function)
请注意,重命名实际上会删除 from 文件夹中的文件。如果你不想,你可以使用 file.copy
关于r - 如何在 R 中将文件从一个文件夹移动到另一个文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61781736/