Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 576
Rappster Avatar asked Jun 12 '13 12:06

Rappster


1 Answers

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.

like image 71
Thell Avatar answered Oct 26 '22 15:10

Thell