File: latestFile.r

package info (click to toggle)
hmisc 5.2-4-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,044 kB
  • sloc: asm: 28,905; f90: 590; ansic: 415; xml: 160; fortran: 75; makefile: 2
file content (26 lines) | stat: -rw-r--r-- 1,014 bytes parent folder | download | duplicates (3)
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
##' Find File With Latest Modification Time
##'
##' Subject to matching on `pattern` finds the last modified file, and if `verbose` is `TRUE` reports on how many total files matched `pattern`.
##' @title latestFile
##' @param pattern a regular expression; see [base::list.files()]
##' @param path full path, defaulting to current working directory
##' @param verbose set to `FALSE` to not report on total number of matching files
##' @return the name of the last modified file
##' @author Frank Harrell
##' @seealso [base::list.files()]
##' @md
latestFile <- function(pattern, path='.', verbose=TRUE) {
  f <- list.files(path=path, pattern=pattern)
  if(length(f) == 1) return(f)
  if(length(f) == 0) {
    warning(paste('no files matching', pattern, 'were found'))
    return(character(0))
  }

  i <- file.info(f, extra_cols=FALSE)
  mtime <- i$mtime
  j <- order(mtime, decreasing=TRUE)[1]
  if(verbose) cat('\nLast modified file: ', f[j],
                  '  (of ', length(f), ' files)\n\n', sep='')
  f[j]
}