'How to import the latest published excel file in R?

I have a folder with several excel files. I can read the file with the readxl function. However, I would like to know if there is any option to read the last published file in the folder. Thanks for the help.



Solution 1:[1]

Using the file.info function as suggested by @vincent-guillemot, we can use the below code to find latest file.

my_folder <- "C:/Users/katti/My Drive/Dropbox/07_Learning"

files_in_folder <- list.files(my_folder, pattern = ".xlsx", full.names = TRUE, recursive = TRUE)

xlsx_df <- file.info(files_in_folder)

xlsx_df$filename <- rownames(xlsx_df)

xlsx_df[xlsx_df$ctime == min(xlsx_df$ctime),"filename"]
#> [1] "C:/Users/katti/My Drive/Dropbox/07_Learning/Blank_CF - Copy.xlsx"

Not the recursive=TRUE argument in list.files. This allows you to also look into subfolders and search for .xlsx files. Set it to FALSE if you only want to search in current folder.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vishal Katti