find_files {fritools} | R Documentation |
Find Files on Disk
Description
Look for files on disk, either scanning a vector of names or searching for
files with list.files
and throw an error if no files are found.
Usage
find_files(
path = ".",
pattern = NULL,
file_names = NA,
all_files = TRUE,
recursive = FALSE,
ignore_case = FALSE,
find_all = FALSE,
select = NA
)
Arguments
path |
see |
pattern |
see |
file_names |
character vector of file names (to be checked if the files exist). |
all_files |
see |
recursive |
see |
ignore_case |
see |
find_all |
Throw an error if not all files (given by file_names) are found? |
select |
A named list of numerical vectors of maximum length 2 named
|
Details
This is a wrapper to either file.exists
or
list.files
, that ensures that (some) files exists. This may
come handy if you want to perform some kind of file manipulation e.g. with
one of the functions listed under
See Also Other file utilities:.
Value
A character vector of file names.
Note
This is merely a wrapper around file.exists
or
list.files
, depending on whether file_names is
given.
See Also
Other searching functions:
compare_vectors()
,
file_modified_last()
,
fromto()
,
grep_file()
,
missing_docs
,
search_files()
,
search_rows()
,
summary.filesearch()
Other file utilities:
clipboard_path()
,
delete_trailing_blank_lines()
,
delete_trailing_whitespace()
,
develop_test()
,
file_copy()
,
file_modified_last()
,
file_save()
,
get_lines_between_tags()
,
get_mtime()
,
get_unique_string()
,
grep_file()
,
is_files_current()
,
is_path()
,
paths
,
search_files()
,
split_code_file()
,
touch()
Examples
#% create some files
files <- unname(sapply(file.path(tempdir(), paste0(sample(letters, 10),
".", c("R", "Rnw", "txt"))),
touch))
print(files)
print(list.files(tempdir(), full.names = TRUE)) # same as above
#% file names given
find_files(file_names = files[1:3])
##% some do not exist:
find_files(file_names = c(files[1:3], replicate(2, tempfile())))
try(find_files(file_names = c(files[1:3], replicate(2, tempfile())),
find_all = TRUE))
##% all do not exist:
try(find_files(file_names = replicate(2, tempfile())))
#% path given
find_files(path = tempdir())
##% change pattern
find_files(path = tempdir(),
pattern = ".*\\.[RrSs]$|.*\\.[RrSs]nw$|.*\\.txt")
##% find a specific file by it's basename
find_files(path = tempdir(), pattern = paste0("^", basename(files[1]), "$"))
#% file_names and path given: file_names beats path
try(find_files(file_names = tempfile(), path = tempdir()))
#% select by file size:
write.csv(mtcars, file.path(tempdir(), "mtcars.csv"))
find_files(path = tempdir())
find_files(path = tempdir(),
select = list(size = c(min = 1000))
)