Add month name and date to file name

Hi everyone,

I need to update my file name with current month name and year
So it has to look like filename_dec21 how can I do that? it should take month and year from system

Assuming that the current date is required

the_files <- dir()
the_files
#> [1] "calc.csv"   "data.csv"   "slides.csv"
the_month <- lubridate::month(Sys.Date(), label = TRUE)
the_month
#> [1] Dec
#> 12 Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < ... < Dec
the_year <- lubridate::year(Sys.Date())%%2000
suffix <- paste0("_",the_month,the_year)
l <- strsplit(the_files,"[.]")
new_names <- list()
for(i in seq_along(l)) new_names[i] = paste0(l[[i]][1],suffix,".",l[[i]][2])
new_names <- unlist(new_names)
new_names
#> [1] "calc_Dec21.csv"   "data_Dec21.csv"   "slides_Dec21.csv"
for(i in seq_along(the_files)) file.rename(the_files[i],new_names[i])
dir()
[1] "calc_Dec21.csv"   "data_Dec21.csv"   "slides_Dec21.csv"

I would like to have this date to be updated automatically to a file name everyday without manually changing and running the code everyday

See the {cronR} package.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.