pgcudahy
pgcudahy

Reputation: 1601

Rename files to creation date in R

I have an R script where I want to download the current copy of the dataset from a remote database, but backup the local version I've been using, with the file's creation date in the title. In a unix terminal I'd use:

mv dataset.rds dataset.$(date -r dataset.rds +"%Y%m%d").rds

How can I do the same from R? I tried using the following system() function without much luck.

creation_date <- system('date -r dataset.rds +"%Y%m%d"')

Several variations on that also didn't work. It also doesn't appear that the R date() function has an equivalent to the unix version's -r flag. Is there another way to get a file's creation date within R?

Upvotes: 1

Views: 1192

Answers (2)

pgcudahy
pgcudahy

Reputation: 1601

Figured it out using R find time when a file was created

file.info() can give me the creation time using $ctime

file.info("dataset.rds")$ctime

So then combine it with file.rename(), using paste() to combine the new filename, creation date, and file extension

file.rename("dataset.rds",paste("dataset", format(file.info("dataset.rds")$ctime,
 "%Y-%m-%d"), "rds", sep = "."))

Upvotes: 2

smoff
smoff

Reputation: 648

You can use file.info for that. It will give you the time the file was modified, created and accessed the last time.

creation_date <- file.info("dataset.rds")$ctime

Upvotes: 3

Related Questions