Skip to contents

Create Folder Structure from Paths in File

Usage

build_folders_from_file(
  file,
  target_dir,
  pattern = NULL,
  max_depth = NULL,
  encoding = "Latin-1"
)

Arguments

file

path to file containing path strings

target_dir

path to target directory in which to create the folder structure

pattern

regular expression matching the paths from file to be considered

max_depth

maximum folder depth to be considered

encoding

encoding used when reading file

Examples

# Create a vector of example paths
paths <- c("a1/b1", "a1/b2", "a2/b1", "a2/b1/c1")

# Write the example paths to a temporary file
writeLines(paths, file <- tempfile())

# Create a temporary target directory
target_dir <- kwb.utils::createDirectory(file.path(tempdir(), "test"))
#> The directory "/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T//RtmpAqBTLj/test" was created.

# Create the folder structure as defined by the paths in the temporary file
kwb.fakin::build_folders_from_file(file, target_dir)
#> Encodings guessed by readr:
#> [1] "ASCII"
#> Selected encoding: windows-1252
#> Reading paths from '/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T//RtmpAqBTLj/file388256fbadb0' ... ok. (0.00s) 
#> 4 lines have been read.
#> Splitting paths ... ok. (0.00s) 
#> The directory "/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T//RtmpAqBTLj/test" already exists.
#> The directory "/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T//RtmpAqBTLj/test/a1" was created.
#> The directory "/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T//RtmpAqBTLj/test/a1/b1" was created.
#> The directory "/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T//RtmpAqBTLj/test/a1/b2" was created.
#> The directory "/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T//RtmpAqBTLj/test/a2" was created.
#> The directory "/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T//RtmpAqBTLj/test/a2/b1" was created.
#> The directory "/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T//RtmpAqBTLj/test/a2/b1/c1" was created.

# List the directory paths below the target directory
paths_reread <- list.dirs(target_dir, recursive = TRUE, full.names = FALSE)

# Stop if not all paths have been created
stopifnot(all(paths %in% paths_reread))