Copy folder recursive in R

后端 未结 6 844
谎友^
谎友^ 2021-02-13 17:27

Am I missing something in the functions?
I want to copy a folder with some files and another subfolder from one location to another. I tried to use file.copy(from, to,

6条回答
  •  既然无缘
    2021-02-13 18:30

    If you want to copy files that lie inside one folder so that they lie inside of a second folder e.g.

    dir1/file1.txt
    dir1/nested/file2.txt
    

    to:

    dir2/file1.txt
    dir2/nested/file2.txt
    

    Then this code works:

    from <- "dir1"
    to   <- "dir2"
    file.copy(list.files(from, full.names = TRUE), 
              to, 
              recursive = TRUE)
    

    Avoid using recursive=TRUE in the list.files() command otherwise you lose the nesting.

    file.copy(list.files(from, full.names = TRUE, recursive = TRUE), 
              to, 
              recursive = TRUE)
    
    # incorrect
    # dir2/file1.txt
    # dir2/file2.txt
    

    Other answers above didn't help with above scenario, eg:

    file.copy(from, to, recursive=TRUE)
    
    # incorrect - makes:
    # dir2/dir1/file1.txt
    # dir2/dir1/nested/file2.txt
    
    file.copy('(...)/path_to_folder_Z', '(...)/folder_X')
    # overwrites existing folder if in same parent directory
    

提交回复
热议问题