How to preserve file attributes when one copies files in Windows?

43

21

On Linux, we can simply do:

cp -pr directory

How to do that in Windows? Can it be done in Windows Explorer? Any GUI tool suggestions?

It would be the best if I can keep the NTFS permissions and creation/modification/access time. At a minimum, I need to preserve the modification date for the files and the directories. Windows Explorer's copy does not preserve the modification date for directories.

netvope

Posted 2010-05-27T21:51:36.877

Reputation: 4 505

possible duplicate of How to copy files while not modifying the file creation times at all?

– John T – 2010-05-27T23:20:08.990

1I don't think it's a duplicate. File creation time is only one of the file attributes. – netvope – 2010-05-28T01:01:26.750

The answers in that question provide software which will preserve all timestamps, mtime, ctime, and atime. – John T – 2010-05-28T01:11:31.090

1robocopy c:\folderA c:\folderB /mir /it is all you need. Note that this will completely delete c:\folderB and create a new c:\folderB that mirrors c:\folderA. – Pacerier – 2016-05-04T03:54:20.630

This may be of interest: http://support.microsoft.com/kb/310316 . Unfortunately some of these registry keys no longer work in Vista and upwards.

– Amro – 2013-06-07T12:21:25.073

Answers

6

There's no simple way to do this in Windows.  There are several programs that claim to be able to preserve the creation dates of files.
Here are a few programs that might do the trick http://forum.soft32.com/windows/preserve-creation-date-Copy-ftopict361820.html  (Broken link; not in the Wayback Machine).

Using just Windows Explorer it's not possible. If you look at the documentation for CopyFile, CopyFile function (winbase.h), you will see that it avoids the subject.  But CopyFile function (Windows Developer Preview) (a 2012 archive of the same URL) says:

File times semantics

This article should document semantics with respect to file creation/modification/access times.

Creation time: if the target file already exists, its' creation time is preserved, otherwise it is set to the current system time.
Last Modification time: always copied from modification time of the source file.
Last Access time: always set to the current system time.

Daisetsu

Posted 2010-05-27T21:51:36.877

Reputation: 5 195

23

RoboCopy can preserve the mtime for directories with the /DCOPY:T switch, but this only works in Vista and newer. It can also preserve ACLs. Alternatively, FastCopy can be configured to preserve directory mtimes, ACLs, and ADSes.

afrazier

Posted 2010-05-27T21:51:36.877

Reputation: 21 316

@afrazier, Why not simply use /mir?

– Pacerier – 2016-05-04T03:54:38.757

2@Pacerier, because the question isn't about mirroring a tree, it's about copying files and folders and preserving as much metadata as possible. Not only does /mir remove data from the destination folder, you still needed the /dcopy:t to preserve directory mtimes and /copyall to preserve more than just the file timestamps. – afrazier – 2016-05-04T13:29:38.770

Just be careful because if you use the MOVE file switch, it doesn't preserve modified times. Use copy, then delete the source. – Jon – 2017-02-23T03:50:16.387

In Windows robocopy is the recommended way to do this since it not only copies file/folder attributes but also allows for proper file integrity and error handling during the entire copy process. – megamorf – 2014-01-01T11:15:41.490

@megamorf: In what way does robocopy do anything special? It doesn't do any checksumming or hashing of the data, it relies on the OS to report errors just like almost any other program. – afrazier – 2014-01-01T17:44:29.413

I agree that as of now robocopy has no pre-post checksum comparison feature.

But in general, robocopy will retransmit the entire file when an error occurs. If you want it to resume writing the rest of a file to the destination after an error occurred without transmitting the entire file again you specify the /Z switch. – megamorf – 2014-01-01T18:25:48.293

21

... or you can just make a zip archive (with compression method set to just "Store"), and then unpack the files where you wanted to copy them. The date/time attributes are the same as the original files.

Silkworm

Posted 2010-05-27T21:51:36.877

Reputation: 211

Cool idea. But does this copy over "the other stuff" like file properties and etc etc? – Pacerier – 2016-05-04T02:36:53.853

2This does not always work. With 7-zip for example, using 7zip compression doesn't preserve one of the file times (I forget which). It does not preserve any directory times (as I recall), With ZIP, you can preserve both created and modified times, but 7zip only keeps some of the times depending on how you unzip: whether you drag and drop or extract from the program menu. Test well, young grasshopper. – Jon – 2016-10-12T03:30:15.907

3Thank you very much, this worked nice for me. I used 7zip, with ZIP format (creation date is not preserved in 7Z format). – AFract – 2017-01-15T09:19:32.303

Could you add the specific command you would use to do this? An example of the specific command to use the compression method you suggested? This would make it more of a complete Answer", as opposed to more of a suggestion or comment. It would also save countless people from having to perform a search to figure out how to accomplish your suggestion. Also, as a bonus, a link to docs or an article with more info about this method would be awesome. Providing all the info saves everyone else from reinventing the (search) wheel, and can dramatically increase upvotes. We thank you! – SherylHohman – 2019-03-28T18:07:27.037

10

Robocopy command line tool (comes with most versions of windows)
On my windows 10 machine it is located at C:\Windows\System32

From Wikipedia, it can be used with the following set of options to preserve file and directory timestamps, as well as other attributes:

Robocopy C:\some_directory D:\some_directory /DCOPY:T /COPYALL /E /R:0

  • /DCOPY:T preserve original Directories' Timestamps (requires version XP026 or later)
  • /E: Copy directories recursively, including Empty Directories
  • /R:0: do not retry locked files (the number of retries on failed copies default value is 1 million),

  • /COPYALL: Copy all file information,
    (equivalent to /COPY:DATSOU, where

    • D=Data,
    • A=Attributes,
    • T=File Timestamps,
    • S=Security=NTFS ACLs,
    • O=Owner info,
    • U=Auditing info),

If you only want to preserve date and directory timestamps, but do not want empty subdirectories copied over and do not want other attributes preserved then you may use:
Robocopy C:\some_directory D:\some_directory /DCOPY:T /COPY:T /S

where /S copies non-empty subdirectories

To see the full syntax, type robocopy /? at a command prompt, or take a look at these blog posts: https://ss64.com/nt/robocopy.html and https://techjourney.net/robocopy-syntax-command-line-switches-and-examples/

An example of a command I recently ran:
Robocopy D:\ C:\D_backup /DCOPY:T /COPYALL /E /R:0 /ZB /ETA /TEE /V /FP /XD D:\$RECYCLE.BIN /XD "D:\System Volume Information" /LOG:C:\D_backup_robocopy.LOG /MIR

Notice how excluding 2 subdirectories requires the /XD flag to be used twice. Also, the second directory exclusion uses quotes because there are spaces in the directory name.

Finally, you can add the /L flag to test the command before doing it for real. In this way you can verify it won't copy (or remove) any directories/files that you do not want, and that there are no errors in your command.

Edit: gotcha when copying from a Root Directory (aka drive) to a Folder (non-root directory)

After running the example command above, the directory was not visible! (Even though show hidden files and show system files were checked in Windows 10!) The following command as per https://serverfault.com/a/455029/399723 fixed this:

attrib -h -s C:\D_backup

Alternatively, adding the /A-:SH "unsets the Hidden Attribute from System files", thus may prevent the directory from becoming hidden in the first place, as per https://serverfault.com/a/565804/399723. See also https://ss64.com/nt/robocopy.html.
This descussion sheds more light on this option as a solution.

Robocopy D:\ C:\D_backup /A-:SH /DCOPY:T /COPYALL /E /R:0 /ZB /ETA /TEE /V /FP /XD D:\$RECYCLE.BIN /XD "D:\System Volume Information" /LOG:C:\D_backup_robocopy.LOG /MIR

Other Software:

Alternatively, you can download a GUI version of robocopy if you don't want to use the command line.

Another windows utility is XCopy, though it is included in windows 10, it is being deprecated. See the link for gotchas.

The Wikipedia page for RoboCopy (linked at top of this post) lists other software that can be used.

SherylHohman

Posted 2010-05-27T21:51:36.877

Reputation: 321

1Good answer. Should be the chosen one, as the first one is kinda outdated. Used robocopy since Windows 7 to trim empty directoeies from client's backups, however, I THINK it was available since Windows XP – DGoiko – 2019-03-28T17:33:31.960

@DGoiko I think you're right. I used it extensively with Windows 7 for sure. Unfortunately, buying special Windows 7 Pro laptops is no longer an option. So I now have Win 10. I don't think I ever looked when I had XP, but it's been around a long time. Just Not so well known. – SherylHohman – 2019-03-28T20:37:17.950

Robocopy GUI is excellent. – Eddie Kumar – 2019-05-10T16:13:00.060

4

Synchronize It! 3.5 keeps all timestamps (files and folders, modification and creation). It's the only software I know on Windows XP which does that flawlessly, beside Robocopy, and I've tried many. Beware, though, it can produce corrupted files with source files downloaded using software like FlashGet or Orbit Downloader (the resulting files have the same size but only 25kB is actually copied - the rest is filled with zeros). I asked the creator of the software if he could figure out why, but he had no clue; I guess it has to do with such software downloading files in small packets (intended to optimize speed) and my data partition being hugely fragmented.

Robocopy XP026 is indeed included in Windows Vista, but works on Windows XP. Search "Robocopy XP026", or I can send the file here as it's not very easy to find. Or you can install Robocopy GUI and then search for the robocopy.exe file in the System32 folder, so as to use it from the command line. The file I have is 208kB and the version number is 5.1.2600.26.

There seems to be a bug with the "backup mode", though:

http://msmvps.com/blogs/martinzugec/archive/2008/03/03/ugly-bug-in-robocopy-ignoring-security-on-file-level.aspx

Robocopy version XP027 apparently no longer has this bug but it doesn't work on Windows XP.

Gabriel

Posted 2010-05-27T21:51:36.877

Reputation: 61

File download utilities (like Bittorrent) allocate the full size of the file on disk (filled with 0), and download sections from multiple sources, storing them in the right place in the file. The files when started will have many 0000. Either the file was never completely downloaded or Synchronize It! saw too many 0000 and gave up the copy. – Chloe – 2013-03-01T19:18:54.343

Thanks! I used Synchronize It! It's so fast too! SyncToy and Unison were messing up the file modification times. Unison was corrupting Cygwin symlinks. SyncToy was even copying 'My Music' which turned out to be in a junction on the destination, but when I refreshed, it kept wanting to copy over again, even though everything was the same! – Chloe – 2013-03-02T07:32:48.793

3

RoboCopy should be able to do it with the COPYALL switch. You can grab the GUI version from Microsoft Technet.

Russ Warren

Posted 2010-05-27T21:51:36.877

Reputation: 2 523

1

FastCopy can do this and it is free.

yarun can

Posted 2010-05-27T21:51:36.877

Reputation: 800

1

xcopy *.* C:\temp\Pics /c /d /e /h /i /k /q /r /s /x /y

I am not sure which switch maintains the file date but this works. Then I zip the C:\temp\Pics directory which also maintains file dates as mentioned earlier.

dmaier

Posted 2010-05-27T21:51:36.877

Reputation: 11

It fits by also maintaining timestamps for the [zipped] files, something that seems to occupy the person who asked the question. You're welcome, Scott. This happens to make for a useful answer, if not the correct one. – amn – 2015-08-20T16:31:53.543

This does preserve file date/time, but does not do so for folder. – barneco – 2017-09-08T17:44:50.427

1How does zipping the copy fit into this discussion? – Scott – 2013-08-19T20:01:12.317

1

The simplest way for me, so far, is the "send to" method. It does not need any external applications. In my experience, it retains all of the original file attributes.

The method I used before, was the compression/zip folder method. Though this is surely to take longer since the files are not only copied, but processed and compressed, both when compressing and decompressing.

%appdata%\Microsoft\Windows\SendTo
or. C:\Users\_______\AppData\Roaming\Microsoft\Windows\SendTo

Add a shortcut to the folder where you want them copied, and the item will appear in your sendto menu when you right-click a file you want to copy.

tspmtel

Posted 2010-05-27T21:51:36.877

Reputation: 11

Thanks for sharing the trick of using "Right click > Send to". It works, except for the creation date. Previously, I was using the native xcopy command: xcopy Source Destination /kes. (The kflag allows to preserve the modification timestamp, but the creation date is not preserved). The compression method to zip (or rar) is a good method, but often takes too long. – OuzoPower – 2018-06-21T04:37:24.043

0

It's simple to move folders and directories and still preserving Creation Date. Just hold Shift and drag the folder to the new location (move command). Date Modified will be the present date but Date Created will retain the original date.

Davidus428

Posted 2010-05-27T21:51:36.877

Reputation: 9

3DO NOT DO THIS! This deletes the files from the original folder. Just lost irrecoverable data from my phone by trying to do this (no CTRL+Z does not work). – BadAtPHP – 2017-03-13T12:01:43.410

0

Simple way to move folders and preserving Creation Date & Time use these utilities

http://lantechsoft.com/data-copy.html

Another one where it can preserve dates while copying files.

http://technocomsolutions.com/data-copy-tool.html

Eric

Posted 2010-05-27T21:51:36.877

Reputation: 1

0

xcopy works fine for this. Just specify the /k flag to copy attributes.

Syntax
xcopy Source [Destination][Optional parameters]

The xcopy command can copy one or more files or directory trees from one location to another. The xcopy command is included in all versions of Windows from Windows 95 to Windows 8.

Chris

Posted 2010-05-27T21:51:36.877

Reputation: 127

1I just tried the /K parameter and it did not preserve the creation dates. – CodeManX – 2016-01-04T18:30:00.477

It seems to me as it works sometimes and sometimes doesnt - windows at its best ;) – SebastianH – 2017-01-05T09:14:37.857

0

I achieve this with

rsync -avu --modify-window=3602 source_directory/ target_directory/

(this is really used as a syncing tool from one directory to another mirror and the timewindow option is to prevent issues when windows systems move clocks 1 hour for daylight savings etc. compared to linux systems)

in either cygwin environment inside windows or a linux virtual machine inside windows with windows drives mounted (e.g. with virtualbox)

NB cygwin and virtualbox are both free and enable you to have to a lesser or greater extent linux functionality on windows - they both require a bit of setting up - particularly virtualbox, but have many benenfits....

My own issue was that files have the correct time but directories did not. rsync has sorted out the dates on the directories as well as the dates on the files.

tom

Posted 2010-05-27T21:51:36.877

Reputation: 143

0

This command copies the same time stamp for files. Note it doesn't do exactly the same time stamping for the folders themselves. Also note that I placed quotes around both source (Temp1) and destination (Temp2). This is done in case there are spaces in the paths. I don't have any spaces but I thought I should include it for worst case scenarios.

robocopy /E /copy:DAT "C:\Temp1" "c:\Temp2"

To keep timestamps the same for folders as well:

robocopy /E /copy:DAT /dcopy:DAT "C:\Temp1" "c:\Temp2"

pablo

Posted 2010-05-27T21:51:36.877

Reputation: 1