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 tofile.path(). Otherwise,...is a placeholder for additional arguments passed tolist.files(), such aspattern, excludingfull.names.- .sink
A
characterstring that defines the directory in which files are located.- .folder
(optional) A
characterstring that defines the name of a sub-folder for which to list files (viafile_list()) or summarise file sizes (viafile_size()).- .unit
For
file_size(),.unitis acharacterstring that defines the units of the output (MB,GB,TB).
Value
file_path()returns acharacterstring that defines the file path.file_list()returns an ordered vector of file paths.file_size()returns a number.file_cleanup()returnsinvisible(NULL).
Details
file_path()is a simple wrapper forfile.path()constructs a file path and verifies that it exists.file_list()creates an orderedlistof numbered files. This function expects files to be named1.{.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;
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)
