Automating the download and naming of a file

Hi all. I have a script to download a file:

url <- "https://www.somedomain.com/Content.88007/tkExcelResults?idQuery=1"
destfile <- "/Users/joseperez/Desktop/22-08-2023.xlsx"
download.file(url, destfile)

All OK for now. Now I want to automate the process of downloading this file every day.

file <- "myscript.R"
cmd <- cron_rscript(file)
cron_add(command = cmd, frequency = 'daily', 
         id = 'test', at = '00:00', description = 'download file', tags = c('lab', 'xyz'))

Now, how can I name the file with the date it was created and not have to enter it manually every day (as in the first step)? I hope I have explained correctly.

Hi @jose_perez. One approach is to paste together destfile utilizing Sys.time() and format() to get the current date in the desired format.

destfile = paste0("/Users/joseperez/Desktop/", 
                  format(as.Date(Sys.time()), format = '%d-%m-%Y'), 
                  ".xlsx")

destfile
#> [1] "/Users/joseperez/Desktop/22-08-2023.xlsx"

Created on 2023-08-22 with reprex v2.0.2

This topic was automatically closed 7 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.