-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_latest_file.R
More file actions
executable file
·74 lines (71 loc) · 2.08 KB
/
Copy pathfind_latest_file.R
File metadata and controls
executable file
·74 lines (71 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#' Find the latest version of a file
#'
#' @description
#' This will return the latest created file matching
#' the criteria. It uses [fs::dir_info()] to
#' find the files then picks the one with the latest
#' `birthtime`.
#'
#' @param directory The directory in which to search.
#' @param regexp a
#' [regular expression](https://www.regular-expressions.info/quickstart.html)
#' passed to [fs::dir_info()] to search for the file.
#' @param selection_method Valid arguments are "modification_date"
#' (the default) or "file_name".
#'
#' @return the [fs::path()] to the file
#' @examples
#' \dontrun{
#' find_latest_file(
#' directory = "/conf/linkage/output/lookups/Unicode",
#' regexp = "Scottish_Postcode_Directory_.+?\\.rds"
#' )
#' }
#' @noRd
#' @keywords internal
find_latest_file <- function(directory,
regexp,
selection_method = "modification_date") {
if (selection_method == "modification_date") {
latest_file <- fs::dir_info(
path = directory,
type = "file",
regexp = regexp,
recurse = TRUE
) |>
dplyr::arrange(
dplyr::desc(.data$birth_time),
dplyr::desc(.data$modification_time),
dplyr::desc(.data$path)
) |>
magrittr::extract(1L, )
} else if (selection_method == "file_name") {
latest_file <- fs::dir_info(
path = directory,
type = "file",
regexp = regexp,
recurse = FALSE
) |>
dplyr::arrange(
dplyr::desc(.data$path),
dplyr::desc(.data$birth_time),
dplyr::desc(.data$modification_time)
) |>
magrittr::extract(1L, )
}
if (nrow(latest_file) == 1L) {
cli::cli_alert_info(
"Using the latest available version: {.val {fs::path_file(
fs::path_ext_remove(latest_file$path))}}.
If you require an older version specify an argument `version`."
)
} else {
cli::cli_abort(
"There was no file in {.path {directory}} that matched the
regular expression {.val {regexp}}"
)
}
file_path <- latest_file |>
dplyr::pull(.data$path)
return(file_path)
}