| 1 |
#' List files |
| 2 |
#' |
| 3 |
#' @description |
| 4 |
#' `dir_ls()` is equivalent to the `ls` command. It returns filenames as a |
| 5 |
#' named `fs_path` character vector. The names are equivalent to the values, |
| 6 |
#' which is useful for passing onto functions like `purrr::map_dfr()`. |
| 7 |
#' |
| 8 |
#' `dir_info()` is equivalent to `ls -l` and a shortcut for |
| 9 |
#' `file_info(dir_ls())`. |
| 10 |
#' |
| 11 |
#' `dir_map()` applies a function `fun()` to each entry in the path and returns |
| 12 |
#' the result in a list. |
| 13 |
#' |
| 14 |
#' `dir_walk()` calls `fun` for its side-effect and returns the input `path`. |
| 15 |
#' |
| 16 |
#' @param type File type(s) to return, one or more of "any", "file", "directory", |
| 17 |
#' "symlink", "FIFO", "socket", "character_device" or "block_device". |
| 18 |
#' @param recurse If `TRUE` recurse fully, if a positive number the number of levels |
| 19 |
#' to recurse. |
| 20 |
#' @param recursive (Deprecated) If `TRUE` recurse fully. |
| 21 |
#' @inheritParams path_filter |
| 22 |
#' @param all If `TRUE` hidden files are also returned. |
| 23 |
#' @param fail Should the call fail (the default) or warn if a file cannot be |
| 24 |
#' accessed. |
| 25 |
#' @template fs |
| 26 |
#' @export |
| 27 |
#' @examples |
| 28 |
#' \dontshow{.old_wd <- setwd(tempdir())} |
| 29 |
#' dir_ls(R.home("share"), type = "directory") |
| 30 |
#' |
| 31 |
#' # Create a shorter link |
| 32 |
#' link_create(system.file(package = "base"), "base") |
| 33 |
#' |
| 34 |
#' dir_ls("base", recurse = TRUE, glob = "*.R") |
| 35 |
#' |
| 36 |
#' # If you need the full paths input an absolute path |
| 37 |
#' dir_ls(path_abs("base")) |
| 38 |
#' |
| 39 |
#' dir_map("base", identity) |
| 40 |
#' |
| 41 |
#' dir_walk("base", str) |
| 42 |
#' |
| 43 |
#' dir_info("base") |
| 44 |
#' |
| 45 |
#' # Cleanup |
| 46 |
#' link_delete("base") |
| 47 |
#' \dontshow{setwd(.old_wd)} |
| 48 |
dir_ls <- function( |
| 49 |
path = ".", |
| 50 |
all = FALSE, |
| 51 |
recurse = FALSE, |
| 52 |
type = "any", |
| 53 |
glob = NULL, |
| 54 |
regexp = NULL, |
| 55 |
invert = FALSE, |
| 56 |
fail = TRUE, |
| 57 |
..., |
| 58 |
recursive |
| 59 |
) { |
| 60 |
assert_no_missing(path) |
| 61 |
|
| 62 |
if (!missing(recursive)) { |
| 63 |
recurse <- recursive |
| 64 |
warning( |
| 65 |
"`recursive` is deprecated, please use `recurse` instead", |
| 66 |
immediate. = TRUE, |
| 67 |
call. = FALSE |
| 68 |
) |
| 69 |
} |
| 70 |
|
| 71 |
old <- path_expand(path) |
| 72 |
|
| 73 |
files <- as.character(dir_map(old, identity, all, recurse, type, fail)) |
| 74 |
|
| 75 |
path_filter(files, glob, regexp, invert = invert, ...) |
| 76 |
} |
| 77 |
|
| 78 |
directory_entry_types <- c( |
| 79 |
"any" = -1L, |
| 80 |
"unknown" = 1L, |
| 81 |
"file" = 2L, |
| 82 |
"directory" = 4L, |
| 83 |
"symlink" = 8L, |
| 84 |
"FIFO" = 16L, |
| 85 |
"socket" = 32L, |
| 86 |
"character_device" = 64L, |
| 87 |
"block_device" = 128L |
| 88 |
) |
| 89 |
|
| 90 |
#' @rdname dir_ls |
| 91 |
#' @param fun A function, taking one parameter, the current path entry. |
| 92 |
#' @export |
| 93 |
dir_map <- function( |
| 94 |
path = ".", |
| 95 |
fun, |
| 96 |
all = FALSE, |
| 97 |
recurse = FALSE, |
| 98 |
type = "any", |
| 99 |
fail = TRUE |
| 100 |
) { |
| 101 |
assert_no_missing(path) |
| 102 |
|
| 103 |
if (is.logical(recurse)) { |
| 104 |
if (isTRUE(recurse)) { |
| 105 |
recurse <- -1 |
| 106 |
} else { |
| 107 |
recurse <- 0 |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
type <- match.arg(type, names(directory_entry_types), several.ok = TRUE) |
| 112 |
|
| 113 |
old <- path_expand(path) |
| 114 |
|
| 115 |
.Call( |
| 116 |
fs_dir_map_, |
| 117 |
old, |
| 118 |
fun, |
| 119 |
all, |
| 120 |
sum(directory_entry_types[type]), |
| 121 |
as.integer(recurse), |
| 122 |
fail |
| 123 |
) |
| 124 |
} |
| 125 |
|
| 126 |
#' @rdname dir_ls |
| 127 |
#' @export |
| 128 |
dir_walk <- function( |
| 129 |
path = ".", |
| 130 |
fun, |
| 131 |
all = FALSE, |
| 132 |
recurse = FALSE, |
| 133 |
type = "any", |
| 134 |
fail = TRUE |
| 135 |
) { |
| 136 |
assert_no_missing(path) |
| 137 |
|
| 138 |
old <- path_expand(path) |
| 139 |
|
| 140 |
dir_map(old, fun, all, recurse, type, fail) |
| 141 |
invisible(path_tidy(path)) |
| 142 |
} |
| 143 |
|
| 144 |
#' @rdname dir_ls |
| 145 |
#' @export |
| 146 |
dir_info <- function( |
| 147 |
path = ".", |
| 148 |
all = FALSE, |
| 149 |
recurse = FALSE, |
| 150 |
type = "any", |
| 151 |
regexp = NULL, |
| 152 |
glob = NULL, |
| 153 |
fail = TRUE, |
| 154 |
... |
| 155 |
) { |
| 156 |
assert_no_missing(path) |
| 157 |
|
| 158 |
file_info( |
| 159 |
dir_ls( |
| 160 |
path = path, |
| 161 |
all = all, |
| 162 |
recurse = recurse, |
| 163 |
type = type, |
| 164 |
regexp = regexp, |
| 165 |
glob = glob, |
| 166 |
fail = fail, |
| 167 |
... |
| 168 |
), |
| 169 |
fail = fail |
| 170 |
) |
| 171 |
} |