简体   繁体   中英

Determining age of a file in R

I'm trying to determine age of a file in R. Here is a link that I found: Determining age of a file in shell script

I'm wondering is there any native R way to determine the age of a file in days unit(or hours). Thank you for reading this.

Update (I think this is working):

ageoffile <- function(x,unit="sec"){
  dt = .Internal(Sys.time())-.Internal(file.info(x))$mtime
  if(unit=="hours")
    return(dt/3600)
  else if(unit=="days")
    return(dt/(3600*24))
  else return(dt)
}

You can get the information about your file using the file.info() function, and the current date and time using Sys.time()

info <- file.info("PATH_TO_YOUR_FILE")
Sys.time() - info$mtime

For instance:

> system("touch temp")
> info <- file.info("temp")
> Sys.time() - info$mtime
Time difference of 5.23292 secs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM