Stataq
Stataq

Reputation: 2297

how to get the most recent date from the file names

I have those folders with names of dates under "c:/Document/"

enter image description here

I wonder whether there is a way that I can auto read the most recent date folder in?

Such as get the latest folder names and datadate<-XXX, and then my input can auto point to the most recent folder by input_dir <-Paste0("c:/Document/", datadate, "/") In my example, it will be 20210823 for the datadate.

any suggestions or better way to achieve this goal?

Upvotes: 2

Views: 556

Answers (2)

akrun
akrun

Reputation: 887501

We may use file.info to find the most recent ones opened with ctime etc

 input_dir[which.max(file.info(input_dir)$ctime)]

Upvotes: 2

Vin&#237;cius F&#233;lix
Vin&#237;cius F&#233;lix

Reputation: 8811

You can use the function list.files(path = path,pattern = pattern) to get the name of your folders, where tou can define a path and pattern for the name of your folders. After that you could:

Example

folders <- c("20210603","20210625","20210823")

max_date_folder <-
  folders %>% 
  sort() %>% 
  tail(1)

path <- "C:/Documents"

input_dir <- file.path(path,max_date_folder)

input_dir

[1] "C:/Documents/20210823"

Upvotes: 3

Related Questions