Copy folders from one directory to another in R
Asked Answered
S

4

35

I have two folders (say "A","B") which are in a folder (say "Input"). I want to copy "A" and "B" to another folder (say "Output"). Can I do this in R?

Sigismundo answered 27/7, 2015 at 13:55 Comment(4)
take a look at ?file.copyGillman
That is for individual files right. I want to copy two folders at once.Sigismundo
From in file.copy takes a vector of files, so you can copy two folders at once.Investigation
I am able to copy the files in the folder but not the folderSigismundo
D
31

Copying your current directory files to their new directories

currentfiles is a list of files you want to copy newlocation is the directory you're copying to

If you aren't listing your current files, you'll need to loop through you're working directory

file.copy(from=currentfiles, to=newlocation, 
          overwrite = TRUE, recursive = FALSE, 
          copy.mode = TRUE)

This is for deleting your old files

file.remove(currentfiles)
Diarmit answered 27/7, 2015 at 14:1 Comment(3)
I am able to copy the files in the folder but not the folder. I removed the recursive part of the code.Sigismundo
Argument overwrite is logical according to ?file.copy documentation. Change to either TRUE or FALSE.Lombard
as OP didn't react I edited the post, I've set overwrite to TRUE, meaning this action may be destructive, use wih caution or set to FALSETwentieth
H
23

I am late. This is my simple approach that gets things done. In R,

current_folder <- "C:/Users/Bhabani2077/Desktop/Current"
new_folder <- "C:/Users/Bhabani2077/Desktop/Ins"
list_of_files <- list.files(current_folder, ".py$") 
# ".py$" is the type of file you want to copy. Remove if copying all types of files. 
file.copy(file.path(current_folder,list_of_files), new_folder)
Herbivore answered 24/11, 2017 at 4:54 Comment(3)
you've got wrong name for current folder in list_of_files creationCharming
Sorry. My Bad. Fixed the typo now.Herbivore
This answer is clearer and therefore more useful for newbie.Magnoliamagnoliaceous
O
4

All of the solutions I've seen to this question seem to imply a Unix based operating system (Mac & Linux). I think the reason that the response(s) didn't work for OP is that OP may be on Windows.

In Windows, the definition of a file is just that, a file, whereas Unix defines a file as a file or directory. I believe this may be why file.copy() is not working, based on my understanding of the "File Manipulation" R documentation - arguments inputted to file.copy() for the "from" field must be files (not directories), but can be either files or directories for the "to" field.

Oversold answered 16/1, 2019 at 21:2 Comment(0)
H
4

fs package provides an alternative solution that answers the original question precisely

library(fs)

I've created "input" folder with the structure proposed in my working directory

fs::dir_tree()
#> .
#> +-- copy_folder.R
#> +-- copy_folder.Rproj
#> \-- input
#>     +-- A
#>     |   +-- C
#>     |   \-- exampleA.txt
#>     +-- B
#>     |   \-- exampleB.txt
#>     \-- D
fs::dir_copy("input/A", "output/A")
fs::dir_copy("input/B", "output/B")
fs::dir_tree()
#> .
#> +-- copy_folder.R
#> +-- copy_folder.Rproj
#> +-- input
#> |   +-- A
#> |   |   +-- C
#> |   |   \-- exampleA.txt
#> |   +-- B
#> |   |   \-- exampleB.txt
#> |   \-- D
#> \-- output
#>     +-- A
#>     |   +-- C
#>     |   \-- exampleA.txt
#>     \-- B
#>         \-- exampleB.txt

Note that we worked in the general case that "input" folder contains other folders and files than folder “A” and “B”.

If "input" folder only contains folders “A” and “B”, one line of code would be enough:

fs::dir_copy("input", "output")

Created on 2021-07-03 by the reprex package (v2.0.0)

Howerton answered 3/7, 2021 at 19:10 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.