find_file: Find (n-latest) file

View source: R/transfer.R

find_fileR Documentation

Find (n-latest) file

Description

Find particular file within a directory and return its name/path.

Usage

find_file(
  path,
  name_pattern,
  slice_n = NULL,
  date_filter = NULL,
  date_pattern = NULL,
  date_format = NULL,
  file_time = "ctime",
  ...
)

Arguments

path

File path that contains files of interest (can look into child folders using recursive = TRUE).

name_pattern

Basic regular expression to discover names within a file path.

slice_n

Grab the n-th file from the list (descending order by date). Default set to NULL, entire list is returned. Set to 1 if want just the latest match returned.

date_filter

Reduce set of discovered file based upon a date threshold (default: NULL, no filtering occurs).

date_pattern

Regular expression to search file name for date, parsed with date_format parameter.

date_format

POSIX formatted dates or date/times (e.g. "%Y-%m-%d %H-%M").

file_time

Character vector, one of mtime, ctime, atime, passed to file.info (default: ctime).

...

Additional parameters passed to list.files.

Details

There are two ways to discover the n-th file of interest. Based upon the file name search pattern, the date is sorted either by creation date (ctime) or a provided pattern (date_pattern) and format (date_format) within the file name itself.

Value

Returns character vector, file name.

Examples

## Not run: 
# Find latest file for particular excel file using basic pattern
latest_file <- find_file("/taget_folder",
                 "^target_name.*\\.xlsx",
                 recursive = TRUE)

# Find latest .rds file by the date in the file name itself
latest_file <- find_file(path = "./taget_folder",
                 name_pattern = "^target_data_(\\d{4}\\-\\d{2}\\-\\d{2})[ ]({1}\\d{2}\\-\\d{2})\\.rds$",
                 date_pattern = "(\\d{4}-\\d{2}-\\d{2}\\s\\d{2}-\\d{2})",
                 date_format = "%Y-%m-%d %H-%M",
                 date_filter = lubridate::today()-10, # Only keep less than today and slice the top value
                 full.names=FALSE)

## End(Not run)

al-obrien/farrago documentation built on April 14, 2023, 6:20 p.m.