7

In order to ease the manual copying of large file amounts, I often use FreeFileSync. I noticed that it preserves the original file information such as when a file was created, last modified etc.

Now I need to regularly copy tons of files in batch mode and I'd like to do it in R. So I wondered if R is capable of preserving that information as well. AFAIU, file.rename() and file.copy() alter the file information, e.g. the times are set to the time the files were actually copied.

Is there any way I can restore the original file information after the files have been copied?

jww
  • 97,681
  • 90
  • 411
  • 885
Rappster
  • 12,762
  • 7
  • 71
  • 120
  • What's your operating system? – Matthew Plourde Jun 12 '13 at 12:55
  • On Unix-like operating systems, you can use `rsync -a`, `cp -a`, or even `tar` (there are `tar` and `untar` commands in R, so it may work on Windows as well). – Vincent Zoonekynd Jun 12 '13 at 13:29
  • @MatthewPlourde: sorry, forgot. It's Windows 7 (64bit) – Rappster Jun 12 '13 at 14:18
  • @VincentZoonekynd: thanks, I will have a look at them – Rappster Jun 12 '13 at 14:18
  • 1
    Good question. `rsync` on Windows (which comes with Rtools, for instance) doesn't seem to support the "extended attributes" `-X` option which preserves file creation times on other OS's. The closest thing I see is to use `zip()` and then `unzip(..., setTimes=TRUE)`. It's not a perfect substitute though. It seems to change some times by 1 second for some reason, and requires more attention to the current working directory, destination directory, etc. – Josh O'Brien Jun 12 '13 at 21:14
  • @JoshO'Brien: thanks for the info, especially for the pointer to `rsync` on Windows! – Rappster Jun 12 '13 at 22:18
  • `file.rename()` does preserve date modified and date created (at least for me on Win7 64 bit). I use `file.rename()` instead of `file.copy()` for that reason. But obviously you can't use it copy just rename or move. – Chris Holbrook Jun 13 '13 at 19:56
  • @ChrisHolbrook: right, I forgot to explicitly state that I'm after *copying* files from one drive to another – Rappster Jun 14 '13 at 12:25

1 Answers1

1

Robocopy via system2() can keep the timestamps.

> cmdArgs<- paste( normalizePath( file.path(getwd()), winslash="/"),
 normalizePath( file.path(getwd(), "bkup"), winslash="/" ),
  "*.txt",
 "/copy:DAT /V" )
> system2( "robocopy.exe", args=cmdArgs )

Robocopy has a slew of switches for all different types of use cases and can accept a 'job' file for the params and file names. The ability of R to call out using system could also be used to execute an elevated session (perhaps the easiest would be by using a powershell script to call Robocopy) so that all of the auditing info (permissions and such) could be retained as well.

Thell
  • 5,883
  • 31
  • 55