r - 使用 R 将多个文件从多个文件夹复制到单个文件夹

标签 r file-manipulation

嘿我想问如何使用R语言将多个文件夹中的多个文件复制到单个文件夹中

假设有三个文件夹:

  • 桌面/文件夹_A/task/sub_task/
  • 桌面/文件夹_B/task/sub_task/
  • 桌面/文件夹_C/task/sub_task/

  • 在每个 sub_task 文件夹中,有多个文件。我想复制 sub_task 文件夹中的所有文件并将它们粘贴到桌面上的新文件夹中(让我们将此新文件夹命名为“all_sub_task”)。谁能告诉我如何在 R 中使用循环或应用函数来做到这一点?提前致谢。

    最佳答案

    这是一个 R 解决方案。

    # Manually enter the directories for the sub tasks
    my_dirs <- c("desktop/folder_A/task/sub_task/", 
                 "desktop/folder_B/task/sub_task/",
                 "desktop/folder_C/task/sub_task/")
    
    # Alternatively, if you want to programmatically find each of the sub_task dirs
    my_dirs <- list.files("desktop", pattern = "sub_task", recursive = TRUE, include.dirs = TRUE)
    
    # Grab all files from the directories using list.files in sapply
    files <- sapply(my_dirs, list.files, full.names = TRUE)
    
    # Your output directory to copy files to
    new_dir <- "all_sub_task"
    # Make sure the directory exists
    dir.create(new_dir, recursive = TRUE)
    
    # Copy the files
    for(file in files) {
      # See ?file.copy for more options
      file.copy(file, new_dir)
    }
    

    编辑为以编程方式列出 sub_task目录。

    关于r - 使用 R 将多个文件从多个文件夹复制到单个文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34345062/

    相关文章:

    r - R 中 group 的每个变化增加 1

    r - R 领带中的 Spearman cor.test 是否已更正?

    linux - 在linux中使用tac命令查找和反转多个文件中的文本

    bash - 使用 tr 删除多个文件中的换行符?

    R正则表达式匹配已知字符以外的东西

    图中数据点的 R 箭头标记

    r - 在 r 中使用 dplyr 建立组之间的差异

    python - Writeline() 在文件末尾添加空行 [Python]

    unix - 如何在考虑用户权限的情况下执行文件/目录操作?

    linux - 在 Linux 中从包含文件夹名称的多个文件夹复制文件