Copy multiple files from multiple folders to a single folder using R
Asked Answered
S

2

5

Hey I want to ask how to copy multiple files from multiple folders to a single folders using R language

Assuming there are three folders:

  1. desktop/folder_A/task/sub_task/
  2. desktop/folder_B/task/sub_task/
  3. desktop/folder_C/task/sub_task/

In each of the sub_task folder, there are multiple files. I want to copy all the files in the sub_task folders and paste them in a new folder (let's name this new folder as "all_sub_task") on desktop. Can anyone show me how to do it in R using the loop or apply function? Thanks in advance.

Sheerness answered 17/12, 2015 at 22:0 Comment(0)
R
11

Here is an R solution.

# 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)
}

Edited to programmatically list sub_task directories.

Richter answered 17/12, 2015 at 22:30 Comment(7)
Thanks. I want to ask if there is a way to do a loop function to create my_dirs since there are many folders (so not just folder A, B and C). It will be quite a task to type out each path.Sheerness
for(dir in my_dirs) { dir.create(dir, recursive = TRUE) }?Richter
But this code does not create the my_dirs. I know I can use list.files to list the name of folder in desktop. But I don't know how can I use the loop to paste each folder names to make a list of "desktop/(folder name)/task/subtask/".Sheerness
For the example in your comment, my_dirs <- list.dirs("desktop", pattern = "subtask", recursive = TRUE) should work then. See my edit.Richter
It does not work. It returns "Unused Argument" and I think it referes to pattern.Sheerness
Oops - you can try list.files("desktop", pattern = "subtask", recursive = TRUE, include.dirs = TRUE) instead of list.dirs - it should still list the directories.Richter
Is there a way to copy files with duplicate filenames into one directory and retain both files by having the duplicate(s) rename automatically?Marin
U
2

This code should work. This function takes one directory -for example desktop/folder_A/task/sub_task/- and copies everything there to a second one. Of course you can use a loop or apply to use more than one directory at once, as the second value is fixed sapply(froms, copyEverything, to)

copyEverything <- function(from, to){
  # We search all the files and directories
  files <- list.files(from, r = T)
  dirs  <- list.dirs(from, r = T, f = F)    


  # We create the required directories
  dir.create(to)
  sapply(paste(to, dirs, sep = '/'), dir.create)

  # And then we copy the files
  file.copy(paste(from, files, sep = '/'), paste(to, files, sep = '/'))
}
Utile answered 18/12, 2015 at 18:6 Comment(1)
Is there a way to copy files with duplicate filenames into one directory and retain both files by having the duplicate(s) rename automatically?Marin

© 2022 - 2024 — McMap. All rights reserved.