Wilcar
Wilcar

Reputation: 2513

How to change randomly the dates of creation and modification of files

I need to change the files creation / modification dates for an exercice.

According to http://theautomatic.net/2018/12/18/how-to-change-file-last-modified-date-with-r/ , I can find files info with file.info()function.

I can also change the dates modification of all the files in a folder combining sapply and Sys.setFileTime :

 sapply(list.files("path", full.names = TRUE), 
              function(FILE) Sys.setFileTime(FILE, "1975-01-01"))

How can I also change date of creation ? (ctime <S3: POSIXct>)

What I expect : for the exercice : I want to change randomly the dates of creation and modification of files (in a range of a year, files are not are created at the same day)

What I tried (changing only date of modification) :

     sapply(list.files("C:/Users/cariou-w/Nextcloud/sync-uncloud/1920-1921/master/web_scrapping/plans", full.names = TRUE), 
      function(FILE) Sys.setFileTime(FILE, paste0("2020-11-", sample(days,1))))

Upvotes: 0

Views: 381

Answers (1)

bcarlsen
bcarlsen

Reputation: 1441

There is no generic cross platform way to change file creation time in R - many file systems do not even track it. You can use system or system2 to call whatever the appropriate shell command is for you. E.g. on OS X, touch -t 202001011234 /path/to/my/file

Upvotes: 1

Related Questions