Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Equivalent of cp dir1/* dir2/ with file_copy #117

Closed
romainfrancois opened this issue Jun 20, 2018 · 3 comments
Closed

Equivalent of cp dir1/* dir2/ with file_copy #117

romainfrancois opened this issue Jun 20, 2018 · 3 comments
Labels
bug an unexpected problem or unintended behavior feature a feature request or enhancement

Comments

@romainfrancois
Copy link

This is probably related to #102, I'm trying to copy the content of a directory into another directory with dir_ls() %>% file_copy() but the directories are empty on the target directory:

library(fs)
library(magrittr)

source <- system.file(package = "fs")
target <- tempfile()
dir_create(target)

source %>% 
  dir_ls() %>% 
  file_copy(target)

dir_ls(path(source, "tests"))
#> /Library/Frameworks/R.framework/Versions/3.5/Resources/library/fs/tests/testthat
#> /Library/Frameworks/R.framework/Versions/3.5/Resources/library/fs/tests/testthat.R
dir_ls(path(target, "tests"))
#> character(0)

dir_ls(recursive = TRUE) made it worse as everything ends up flat in the target directory.

@jimhester
Copy link
Member

If you are expecting a tests directory in the target you should use dir_copy().

library(fs)

source <- system.file(package = "fs")
target <- tempfile()
dir_create(target)
dir_copy(path(source, "tests"), target)
dir_ls(path(target, "tests"))
#> /var/folders/dt/r5s12t392tb5sk181j3gs4zw0000gn/T/Rtmpl9DMnA/filef6aa5c145329/tests/testthat
#> /var/folders/dt/r5s12t392tb5sk181j3gs4zw0000gn/T/Rtmpl9DMnA/filef6aa5c145329/tests/testthat.R

Created on 2018-06-20 by the reprex package (v0.2.0).

@romainfrancois
Copy link
Author

I guess I'm looking for something like this.

library(fs)
library(magrittr)
library(purrr)
#> 
#> Attaching package: 'purrr'
#> The following object is masked from 'package:magrittr':
#> 
#>     set_names

source <- system.file(package = "fs")
target <- tempfile()
dir_create(target)

info <- dir_info(source)
walk2(info$path, as.character(info$type), ~{
  if ( .y == "file" ) file_copy(.x, target)
  if ( .y == "directory" ) dir_copy(.x, target)
})

identical(
  path_file(dir_ls(source, recursive = TRUE)), 
  path_file(dir_ls(target, recursive = TRUE))
)
#> [1] TRUE

Created on 2018-06-20 by the reprex package (v0.2.0).

@jimhester jimhester added bug an unexpected problem or unintended behavior feature a feature request or enhancement labels Jun 29, 2018
@gaborcsardi
Copy link
Member

Is this what you want?

target <- tempfile()
❯ dir_copy("tests", target)
❯ dir_tree(target)
/var/folders/59/0gkmw1yj2w7bf2dfc3jznv5w0000gn/T//RtmpPibWyG/file13fc48d529fb
├── spelling.R
├── testthat
│   ├── blns.txt.xz
│   ├── helper.R
│   ├── ref-tree-1
│   ├── ref-tree-2
│   ├── ref-tree-3
│   ├── test-access.R
│   ├── test-copy.R
│   ├── test-create.R
│   ├── test-delete.R
│   ├── test-file.R
│   ├── test-file_exists.R
│   ├── test-fs_bytes.R
│   ├── test-fs_path.R
│   ├── test-fs_perms.R
│   ├── test-id.R
│   ├── test-is.R
│   ├── test-link.R
│   ├── test-list.R
│   ├── test-path.R
│   ├── test-path_package.R
│   ├── test-sanitize.R
│   ├── test-temp.R
│   ├── test-tree.R
│   └── test-utils.R
└── testthat.R

If target might already exist, then you can set overwrite = TRUE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants