copyDirectory: Copy Directory/Directories

Description Usage Arguments Note Author(s) References See Also Examples

Description

Copies the contents of a directory, possibly recursively.

Usage

1
2
copyDirectory(from, to, pattern, ensure_to = FALSE, overwrite = FALSE,
  recursive = TRUE, ...)

Arguments

from

Signature argument. Object containing source directory information.

to

Signature argument. Object containing target directory information.

pattern

Signature argument. Filter for matching directory specific directory content.

ensure_to

logical. Dimension: 1. TRUE means non-existing target directories in to are created, FALSE means they will trigger an error.

overwrite

logical. Dimension: 1. TRUE means existing directories are overwritten, FALSE means they are not.

recursive

logical. TRUE means subdirectories and their content are copied, FALSE means they are not.

...

Further arguments to be passed to subsequent functions/methods.

Note

Target directories that don't exist are created, silently (assuming write permission).

Author(s)

Janko Thyson janko.thyson@rappster.de

References

https://github.com/Rappster/filesystr

See Also

copyDirectory-character-character-character-method

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
## Not run: 

## Directory preparations //
to <- file.path(tempdir(), "copyDirectory", c("to1", "to2"))
sapply(to, dir.create, recursive=TRUE, showWarnings=FALSE)

## Utility functions //
.carefulCleanup <- function(x, pattern=basename(tempdir()), content.only=TRUE) {
  out <- sapply(x, function(ii) {
      out <- FALSE
      if (grepl(pattern, ii)) {
        out <- !as.logical(unlink(ii, recursive=TRUE, force=TRUE))
        if (out & content.only) {
          dir.create(ii, recursive=TRUE, showWarnings=FALSE)    
        }
      }        
      out
    })
  invisible(out)
}

## Open directories in file system browser //
.openRessource(to[1])
.openRessource(to[2])

## One source, one target //
copyDirectory(from = R.home("etc"), to = to[1])
.carefulCleanup(to[1])

## One source, two targets //
copyDirectory(from = R.home("etc"), to = to)
.carefulCleanup(to)

## Two sources, one target //
## Combines content of 'etc' and 'tests'. If there would be any conflicts,
## the would result in an error as 'overwrite = FALSE'.
## Otherwise, existing content is overwritten.
copyDirectory(from = c(R.home("etc"), R.home("tests")), to = to[1])
.carefulCleanup(to)

## Two sources, two targets //
copyDirectory(from = c(R.home("etc"), R.home("tests")), to = to)
.carefulCleanup(to)

## Non-recursive //
## Only files directly below 'from' and actual subdirectories are copied, 
## *NOT* their content.
copyDirectory(from = R.home("etc"), to = to[1], recursive = FALSE)

## Subsequent recursive copy, existing content overwritten //
## This will add the actual content of the subdirectories.
copyDirectory(from = R.home("etc"), to = to[1], overwrite = TRUE)
.carefulCleanup(to[1])

## Condition handling //
## Handling of non-existing target directories:
.carefulCleanup(to, content.only = FALSE)
try(copyDirectory(from = R.home("etc"), to = to[1]))
## Existence of target root directories can be ensured:
copyDirectory(from = R.home("etc"), to = to[1], ensure_to =TRUE)


## End(Not run)

rappster/filesystr documentation built on May 26, 2019, 11:17 p.m.