r - 如何在 R 中将文件从一个文件夹移动到另一个文件夹?

标签 r

我知道之前有人问过这个问题,但与仅使用 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/

相关文章:

r - 正确地将 for 循环转换为并行循环

r - 将 purrr::walk2() 应用于管道末端的 data.frames 的 data.frame

r - Dplyr 加入 by=(a = b),其中 a 和 b 是包含字符串的变量?

r - 如何获得负数的平方根,例如 sqrt(-3)?

r - 将最后一个值添加到数据框的顶部。

r - 估计命令如何查找 R 公式中的变量名称?

r - 如何尽快基于组连接行

r - tidyr - 展开多列

r - 重复执行矩阵乘法的有效方法

R网格额外: modify theme on the fly for a single tableGrob?