Self-regualted Learning within the Personal Learning Environment (SRL⏋PLE)

- In which I will write about my observations as a PhD student

Selecting most recent file in a directory using R

I'm attending an introductory course in the programming language R. Introduction to R: a free software environment for statistical computing and graphics at UiO.

For my project at the course I store my data in a SQLite database – basically a database stored in a single file. I often copy the file with a new name. If something unintended should happen in the database I can always go back to the previous version. I name the file with the current date. The pattern is like: YYYYMMDD[...].db and the result is: 20180317_project.db.

To make it easy to import the data from the right database file I created a small script that selects the most recent database file from my directory:

# select the most recent file in a directory
dir.dir <- "~/project/db"
dir.files <- dir(dir.dir, pattern="^[0-9]{8}.+db$")
dir.files.dates <- sort(substr(dir.files, 1, 8), decreasing = TRUE)
dir.mostrecentfile <- dir.files[grep(dir.files.dates[1], dir.files)]

Explanation:

  1. Set the directory to ~/project/db
  2. Get all the files with the pattern ^[0-9]{8}.+db$, so starting with 8 digiats and ends with db and with something in between.
  3. Sort the list of files/dates (even though it is not necessary)
  4. Select the the file name from the list using the first element in the sorted list.

I know, it is not rocket science, but it's really helpful.