Skip to contents

These are simple file system utilities.

Usage

file_path(...)

file_list(.sink, .folder = NULL, ...)

file_size(.sink, .folder = NULL, ..., .unit = c("MB", "GB", "TB"))

file_cleanup(.sink)

Arguments

...

In file_path(), ... is passed to file.path(). Otherwise, ... is a placeholder for additional arguments passed to list.files(), such as pattern, excluding full.names.

.sink

A character string that defines the directory in which files are located.

.folder

(optional) A character string that defines the name of a sub-folder for which to list files (via file_list()) or summarise file sizes (via file_size()).

.unit

For file_size(), .unit is a character string that defines the units of the output (MB, GB, TB).

Value

  • file_path() returns a character string that defines the file path.

  • file_list() returns an ordered vector of file paths.

  • file_size() returns a number.

  • file_cleanup() returns invisible(NULL).

Details

  • file_path() is a simple wrapper for file.path() constructs a file path and verifies that it exists.

  • file_list() creates an ordered list of numbered files. This function expects files to be named 1.{.ext}, 2.{.ext}, ..., N.{.ext}. All listed files must share the same file extension.

  • file_size() calculates the total size of files in a directory.

  • file_cleanup() deletes temporary files and or directories recursively;

Author

Edward Lavender

Examples

# Set up example
temp <- tempdir()
sink <- file.path(temp, "patter")
dir.create(sink, recursive = TRUE)
write.table("", file = file.path(sink, "1.csv"))
write.table("", file = file.path(sink, "2.csv"))

# Use `file_path()` to construct & validate file paths
file_path(temp, "patter")
#> [1] "/var/folders/nl/ygb3g6tj24z06jddbqqhj6hw0000gn/T//RtmpwiS0jm/patter"

# Use `file_list()` to list files
file_list(temp, "patter", pattern = "*.csv$")
#> [1] "/var/folders/nl/ygb3g6tj24z06jddbqqhj6hw0000gn/T//RtmpwiS0jm/patter/1.csv"
#> [2] "/var/folders/nl/ygb3g6tj24z06jddbqqhj6hw0000gn/T//RtmpwiS0jm/patter/2.csv"

# Use `file_size()` to summarise file sizes
file_size(temp, "patter")
#> [1] 2.2e-05
file_size(temp, "patter", .unit = "GB")
#> [1] 2.2e-08
file_size(temp, "patter", .unit = "TB")
#> [1] 2.2e-11

file_cleanup(sink)