Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read the file created/modified last in different directories in R

I'd want to read the CSV files modified( or created) most recently in differents directories and then put it in a pre-existing single dataframe (df_total).

I have two kinds of directories to read:

A:/LogIIS/FOLDER01/"files.csv"

On others there a folder with several files.csv, as the example bellow:

"A:/LogIIS/FOLDER02/FOLDER_A/"files.csv"

"A:/LogIIS/FOLDER02/FOLDER_B/"files.csv"

"A:/LogIIS/FOLDER02/FOLDER_C/"files.csv"

"A:/LogIIS/FOLDER03/FOLDER_A/"files.csv"

"A:/LogIIS/FOLDER03/FOLDER_B/"files.csv"

"A:/LogIIS/FOLDER03/FOLDER_C/"files.csv"

"A:/LogIIS/FOLDER03/FOLDER_D/"files.csv"
like image 875
Helio Roots Avatar asked May 11 '17 17:05

Helio Roots


Video Answer


2 Answers

Something like this...

#get a vector of all filenames
files <- list.files(path="A:/LogIIS",pattern="files.csv",full.names = TRUE,recursive = TRUE)

#get the directory names of these (for grouping)
dirs <- dirname(files)

#find the last file in each directory (i.e. latest modified time)
lastfiles <- tapply(files,dirs,function(v) v[which.max(file.mtime(v))])

You can then loop through these and read them in.

If you just want the latest file overall, this will be files[which.max(file.mtime(files))].

like image 118
Andrew Gustar Avatar answered Oct 22 '22 07:10

Andrew Gustar


Here a tidyverse-friendly solution

list.files("data/",full.names = T) %>% 
  enframe(name = NULL) %>% 
  bind_cols(pmap_df(., file.info)) %>% 
  filter(mtime==max(mtime)) %>% 
  pull(value)
like image 23
avallecam Avatar answered Oct 22 '22 06:10

avallecam