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.

ialm
  • 8,510
  • 4
  • 36
  • 48
kelvinfrog
  • 435
  • 1
  • 8
  • 18

2 Answers2

9

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.

ialm
  • 8,510
  • 4
  • 36
  • 48
  • 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. – kelvinfrog Dec 17 '15 at 22:40
  • `for(dir in my_dirs) { dir.create(dir, recursive = TRUE) }`? – ialm Dec 17 '15 at 22:42
  • 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/". – kelvinfrog Dec 17 '15 at 22:55
  • For the example in your comment, `my_dirs <- list.dirs("desktop", pattern = "subtask", recursive = TRUE)` should work then. See my edit. – ialm Dec 17 '15 at 23:02
  • It does not work. It returns "Unused Argument" and I think it referes to pattern. – kelvinfrog Dec 18 '15 at 15:05
  • 1
    Oops - you can try `list.files("desktop", pattern = "subtask", recursive = TRUE, include.dirs = TRUE)` instead of `list.dirs` - it should still list the directories. – ialm Dec 18 '15 at 16:44
  • Is there a way to copy files with duplicate filenames into one directory and retain both files by having the duplicate(s) rename automatically? – Liza Dec 19 '16 at 19:39
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 = '/'))
}
durum
  • 3,316
  • 1
  • 25
  • 30
  • Is there a way to copy files with duplicate filenames into one directory and retain both files by having the duplicate(s) rename automatically? – Liza Dec 19 '16 at 19:40