41

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.

  • possible duplicate of How to copy files while not modifying the file creation times at all? – John T May 27 '10 at 23:20
  • 1
    I don't think it's a duplicate. File creation time is only one of the file attributes. – netvope May 28 '10 at 1:01
  • The answers in that question provide software which will preserve all timestamps, mtime, ctime, and atime. – John T May 28 '10 at 1:11
  • This may be of interest: support.microsoft.com/kb/310316 . Unfortunately some of these registry keys no longer work in Vista and upwards. – Amro Jun 7 '13 at 12:21
  • robocopy 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 May 4 '16 at 3:54

14 Answers 14

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.

  • Link is broken. – Smile4ever Jan 2 '16 at 7:41
21

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.

  • 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 Jan 1 '14 at 11:15
  • @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 Jan 1 '14 at 17:44
  • 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 Jan 1 '14 at 18:25
  • @afrazier, Why not simply use /mir? – Pacerier May 4 '16 at 3:54
  • 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 May 4 '16 at 13:29
19

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

  • Cool idea. But does this copy over "the other stuff" like file properties and etc etc? – Pacerier May 4 '16 at 2:36
  • 2
    This 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 Oct 12 '16 at 3:30
  • 3
    Thank you very much, this worked nice for me. I used 7zip, with ZIP format (creation date is not preserved in 7Z format). – AFract Jan 15 '17 at 9:19
  • 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 Mar 28 at 18:07
6

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.

  • 1
    Good 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 Mar 28 at 17:33
  • @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 Mar 28 at 20:37
  • Robocopy GUI is excellent. – Eddie Kumar May 10 at 16:13
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.

  • 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 Mar 1 '13 at 19:18
  • 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 Mar 2 '13 at 7:32
3

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

1

FastCopy can do this and it is free.

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.

  • 1
    How does zipping the copy fit into this discussion? – Scott Aug 19 '13 at 20:01
  • 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 Aug 20 '15 at 16:31
  • This does preserve file date/time, but does not do so for folder. – barneco Sep 8 '17 at 17:44
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.

  • 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 Jun 21 '18 at 4:37
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.

  • 2
    DO 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 Mar 13 '17 at 12:01
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

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.

  • 1
    I just tried the /K parameter and it did not preserve the creation dates. – CoDEmanX Jan 4 '16 at 18:30
  • It seems to me as it works sometimes and sometimes doesnt - windows at its best ;) – SebastianH Jan 5 '17 at 9:14
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.

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"

protected by Community Jan 25 '15 at 23:17

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.