Search code examples
rfilecopymovesubdirectory

copy many files from subdirectories


I am trying to copy/move files from many subdirectories into one folder:

new.folder <- "path/to/new/folder"
files <- basename(list.files(path, recursive = T))
file.copy(from=files, to=new.folder)

unfortunately I am gettin errors for all files:

In file.copy(from = files, to = new.folder):
  problem copying ./E202.fastq.gz to /path/to/new/folder/E202.fastq.gz: No such file or directory

I would appreciate any help, I am new to R.


Solution

  • Make sure all files exist:

    all(file.exists(files))
    

    Otherwise, use full.names = TRUE

    files <- list.files(path, recursive = TRUE, full.names = TRUE)
    

    Because if you don't, the files will be assumed to be in the current directory

    getwd()
    

    which may or may not be equivalent to path