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

... 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.


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.


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.