Copy files while preserving original file information (creation time etc.)
Asked Answered
H

1

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?

Helms answered 12/6, 2013 at 12:44 Comment(8)
What's your operating system?Pickett
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).Fimbriate
@MatthewPlourde: sorry, forgot. It's Windows 7 (64bit)Helms
@VincentZoonekynd: thanks, I will have a look at themHelms
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.Archiepiscopate
@JoshO'Brien: thanks for the info, especially for the pointer to rsync on Windows!Helms
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.Edgardo
@ChrisHolbrook: right, I forgot to explicitly state that I'm after copying files from one drive to anotherHelms
D
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.

Dibble answered 17/6, 2013 at 4:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.