Skip to contents

Outputs that are saved again at each run are named <prefix>_<YYYY-MM-DD>.<extension>, so that older versions are kept and the most recent one is found by its name:

  • dated_file_name() builds such a name;

  • file_date() reads the date of a file name (YYYY-MM-DD or YYYYMMDD);

  • latest_dated_file() returns the most recent file of a prefix in a folder.

Usage

dated_file_name(prefix, extension, date = Sys.Date(), dir = NULL)

file_date(files)

latest_dated_file(folder, prefix, extension = "rds", full_names = FALSE)

Arguments

prefix

Character. Prefix of the file name.

extension

Character. Extension, without the dot.

date

A date (default today).

dir

Character or NULL. Folder prepended to the name.

files

Character. File names or paths.

folder

Character. Folder to search.

full_names

Logical. Return the path rather than the file name.

Value

dated_file_name(): a file name or path. file_date(): a Date vector (NA when a name has no date). latest_dated_file(): one file name or path, or character(0) when none matches.

Details

Two date formats coexist in older data folders (YYYYMMDD and YYYY-MM-DD); both are read. Ordering by the parsed date, rather than by the file name, avoids taking the first file listed for the latest when a name has the other format. The prefix must be followed directly by the date, so that ecb_text does not match ecb_text_cleaned_<date>.

Examples

dated_file_name("ecb_metadata", "rds", date = as.Date("2026-11-08"))
#> [1] "ecb_metadata_2026-11-08.rds"
file_date(c("ecb_metadata_2026-11-08.rds", "ecb_metadata_20250101.rds", "notes.txt"))
#> [1] "2026-11-08" "2025-01-01" NA          

dir <- file.path(tempdir(), "dated_example")
dir.create(dir, showWarnings = FALSE)
file.create(file.path(dir, c(
  "ecb_metadata_20250101.rds", "ecb_metadata_2026-11-08.rds",
  "ecb_metadata_cleaned_2027-01-01.rds"
)))
#> [1] TRUE TRUE TRUE
latest_dated_file(dir, "ecb_metadata")
#> [1] "ecb_metadata_2026-11-08.rds"