file.backup.RdInserts the date-time of the most recent modification at the end of the file name, before the extension.
file.backup(name, fullpath = FALSE, keep.old = FALSE, verbose = FALSE)A character string for the name of the file.
Return the full directory path to the file. Default FALSE, return only the file name.
If FALSE (default), rename the file. Otherwise, keep old copy.
If TRUE, output warnings and list the files in the output directory when done.
The name of the newly created file.
Return is the new file name that was created, using whatever path information was provided in the file's original name. However, the fullpath argument can be set to TRUE, so a path with the full directory name will be created and returned.
tdir <- tempdir()
owd <- getwd()
setwd(tdir)
system("touch test.1.txt")
system("touch test.2.txt")
system("touch test.3.txt")
system("touch test.4.txt")
system("touch test.5.txt")
## note: no extension next
system("touch test.6")
list.files()
#> [1] "downlit" "test.1.txt" "test.2.txt" "test.3.txt" "test.4.txt"
#> [6] "test.5.txt" "test.6"
file.backup("test.1.txt")
#> [1] "test.1-20251014-2024.txt"
file.backup("test.2.txt", fullpath=TRUE)
#> [1] "/tmp/RtmpplWg8J/test.2-20251014-2024.txt"
list.files()
#> [1] "downlit" "test.1-20251014-2024.txt"
#> [3] "test.2-20251014-2024.txt" "test.3.txt"
#> [5] "test.4.txt" "test.5.txt"
#> [7] "test.6"
setwd(owd)
file.backup(file.path(tdir, "test.3.txt"))
#> [1] "/tmp/RtmpplWg8J/test.3-20251014-2024.txt"
## Next should be same path because input had a full path
file.backup(file.path(tdir, "test.4.txt"), fullpath=TRUE)
#> [1] "/tmp/RtmpplWg8J/test.4-20251014-2024.txt"
file.backup(file.path(tdir, "test.5.txt"), fullpath = TRUE, verbose = TRUE)
#> File list of: /tmp/RtmpplWg8J
#> [1] "downlit" "test.1-20251014-2024.txt"
#> [3] "test.2-20251014-2024.txt" "test.3-20251014-2024.txt"
#> [5] "test.4-20251014-2024.txt" "test.5-20251014-2024.txt"
#> [7] "test.6"
#> End of file list
#> [1] "/tmp/RtmpplWg8J/test.5-20251014-2024.txt"
file.backup(file.path(tdir, "test.6"))
#> [1] "/tmp/RtmpplWg8J/test-20251014-2024.6"