0

I use R. Currently, I need to enter a folder of the computer, and within that folder there are other folders, each of which has numerous excel files, I need to get the most current of each folder.

folder structure:

C: \ book \

C: \ book \ x
C: \ book \ x \ x.xlsx
C: \ book \ x \ xx.xlsx
C: \ book \ x \ xxx.xlsx

C: \ book \ y
...
C: \ book \ z
...

Is there any way to do this optimized?

r2evans
  • 77,184
  • 4
  • 55
  • 96
Theorp
  • 151
  • 8

1 Answers1

2

This is mostly a dupe of one of https://stackoverflow.com/a/24376207/3358227 or Read multiple CSV files into separate data frames. For the latter, I counter with a suggestion to store the frames in a list-of-frames and not individual files, but ... that is contextual and might not be ideal for your particular use.

Regardless, here's an adaptation that incorporates "most recent file":

LF <- list.files(path = "path/to", recursive = TRUE, full.names = TRUE)
recent_LF <- c(by(LF, dirname(LF), function(filenames) {
  filenames[ which.max(file.info(filenames)[,"mtime"]) ]
}))

Edit: sometimes I consider that list.files should default to:

list.files <- function(path, ..., recursive = FALSE, full.names = recursive, ...) {
  # ...
}

I see no value in recursive=TRUE, full.names=FALSE, which is what happened with the first version of this answer.

r2evans
  • 77,184
  • 4
  • 55
  • 96