Programmatically delete the earlies data files from the data folder

GaB

I am trying to delete the earlies (oldest) datasets and keep only 3 of them (the earliest) For example, the viewers by app file, the first 3 datasets are the earlies, as you can see the date attached and the time. The rest 3 of them, are older, and do not want to keep them, good for deletion. The same with log metrics files; the first 3 are the earliest and want to keep them, but the last 3 are the oldest and want to delete them.

Is there a way to programmatically delete, in R, the files I do not need? It would be nice to embed the tidyverse library with this solution.

enter image description here

Here are the code I have written to actually created these data, just so that you are aware of how I have created these.

readr::write_csv(log_metrics,
                     paste0(
                       "data/generated_metrics/log_metrics",
                       format(Sys.time(), "%Y%m%d%H%M"),
                       ".csv"
                     ))
Julien
library(tidyverse)

directory <- "data/generated_metrics" # Files' path

list.files(directory, pattern = "log_metrics", full.names = TRUE) %>%
  # Sort files by creation time
  file.info() %>%
  with(sort(ctime, index.return = TRUE)$ix) %>%
  # Identify files to delete (beyond the desired three earliest files)
  .[-c(1:3)] %>%
  file.remove()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related