Robocopy (Robust File Copy) is a command line folder and file replication tool available as a standard Windows feature since Windows Server 2008. The goal of this post is to give you a quick start guide by providing some Robocopy examples.

Over the past 25 years, since Robocopy first became available as part of the Windows NT4 resource pack, it has not changed much. Some features, such as SMB compression, were added, but the usage is still the same. As Robocopy has many great features, such as the ability to copy NTFS ACLs (permissions), it is one of the best free tools for file copy/mirror operations. This Wikipedia post is a good starting point for getting more information about robocopy.

Before we get to the Robocopy examples, you should know the main limitation of robocopy—it cannot copy open or locked files, as it does not use VSS (Volume Shadow Copy Service).

Help and default options ^

Robocopy itself includes very detailed help, with a description of each option. Simply type robocopy /? to get help displayed.

Robocopy help

Robocopy help

The basic syntax for Robocopy is:

robocopy c:\temp\source c:\temp\destination

This will copy all files, excluding subfolders, from the specified source (c:\temp\source) to the specified destination (c:\temp\destination) with a bunch of default options.

Note: If you have a folder with spaces in the name, simply put quotes around the path (“c:\temp\source with spaces”).

Robocopy default options

Robocopy default options

Let's take a closer look at what the options mean:

  • *.*—Defines a file filter. The first asterisk (*) represents the filename part, and the second asterisk represents the extension. In this case, the command copies all filenames with all extensions.
  • /DCOPY:DA—Defines what to copy for directories. D stands for "data" and A stands for "attributes."
  • /COPY:DAT—Defines what to copy for files. The extra T stands for "timestamps;" DA is the same as for directories.
  • /R:1000000—Defines the number of retries on failed copies, for example, if a file is locked.
  • /W:30—Defines the number of seconds to wait between retries.

As you can see, the number of retries is very large (one million), which is not practical in most cases. If there is a single locked file inside the directory, robocopy will do a million retries and will wait 30 seconds between them, amounting to almost 350 days. I guess this is not what you normally want.

Generally speaking, there are three things you will do using robocopy: copy, move, and synchronize folders and files.

Copy examples ^

The easiest way to copy a folder with all files and subfolders is to run this command:

robocopy c:\temp\source c:\temp\destination /E /DCOPY:DAT /R:10 /W:3

The /E switch tells Robocopy to copy all subfolders, including empty ones. If you don't want to copy empty subfolders, use the /S switch. Note that I have specified /DCOPY:DAT (default is DA), as I usually want the folder timestamps to be the same as the source. I also limit the number of retries and wait times. If there is a file locked, it won't usually be unlocked quickly, and it's easier for me to check the erred files manually later rather than waiting ages for the whole command to complete.

If you are about to copy a large number of files, it might be more useful to save the output to a log file so you can review the results later, even if the command prompt is closed.

robocopy c:\temp\source c:\temp\destination /E /DCOPY:DAT /R:100 /W:3 /LOG:C:\temp\robocopylog.txt

Will give you a text log file with all the information you would normally see on screen.

Redirecting the output to a log file

Redirecting the output to a log file

Note: If the destination directory (c:\temp\destination) already exists, you will see one skipped folder in the results.

In many (if not most) cases, you will need to create a completely identical copy of the source, including the NTFS owner and auditing information. For that, I add /COPYALL to the command, which is equivalent to /COPY:DATSOU.

robocopy c:\temp\source c:\temp\destination /E /COPYALL /DCOPY:DAT /R:100 /W:3

Note that this command has to be run from an elevated command prompt, unless you have turned off UAC, otherwise you'll receive this error message:

ERROR : You do not have the Manage Auditing right.

Error when using /COPYALL without elevation

Error when using /COPYALL without elevation

Move examples ^

Robocopy offers two options for moving files and folders:

  • /MOV—Moves files only, leaving the empty folder structure at the source
  • /MOVE—Moves files and folders

Use the command below to move all files and folders, including empty ones, with all attributes. Note that the source folder will also be deleted.

robocopy c:\temp\source c:\temp\destination /E /COPYALL /DCOPY:DAT /MOVE /R:100 /W:3

Synchronize examples ^

As with move, there are two options to synchronize folders:

  • /PURGE—Deletes the files and folders that are no longer present in the source but without subfolders.
  • /MIR—Is equivalent to /PURGE /E. /MIR also copies all files in subfolders, including empty ones.

Be careful with this command, especially with source and destination folders. If you make a mistake here, you might get files lost in the specified folder.

robocopy c:\temp\source c:\temp\destination /MIR /COPYALL /DCOPY:DAT /R:100 /W:3

Filtering examples ^

Sometimes you might want to copy only specific files, for example, Word documents. This can be done easily by adding the *.docx filter after the destination folder before any other options.

robocopy c:\temp\source c:\temp\destination *.doc

You might also want to exclude some files or folders from the operation. This is done using the /XF and /XF options. To exclude all .doc files, use the command below.

robocopy c:\temp\source c:\temp\destination /XF *.doc

To exclude multiple subfolders, use spaces in the command:

robocopy c:\temp\source c:\temp\destination /XD Subfolder EmptySubfolder

Robocopy includes many more filtering options, which you can find in help under the File Selection Options section.

Networking examples ^

Another great feature of Robocopy is that it works well with UNC paths and can resume operation in case of a network interruption. The command below will copy the files in restartable mode.

robocopy c:\temp\source \\MYSERVER\myshare /Z

Backup mode ^

The last, but definitely not the least, option I will cover today is the backup mode option /B. This option allows administrators to copy files in a special backup mode. This is extremely useful if you are asked to copy folders to which you don't have access. The backup mode uses the Backup and Restore security privilege to bypass the NTFS permissions and copy the files. This again requires a command prompt to be elevated. In addition, your account needs at least SeBackupPrivilege, which is usually granted to the Administrators and Backup Operators groups.

robocopy c:\temp\source c:\temp\destination /E /COPYALL /DCOPY:DAT /B

Final words ^

Robocopy is a very powerful tool for performing various file and folder operations. Many administrators use it on a daily basis for profile migrations and other similar operations. The big advantage is that it's part of the operating system itself. There are dozens of other options that can be used with Robocopy, but to cover them all would be beyond the scope of this post.

avataravataravatar
31 Comments
  1. Samir 2 months ago

    While robocopy is one of the most powerful ways to sync drives, it does fail spectacularly in certain events–dst changes, file systems other than ntfs, and just missing files. It is important to check on it periodically to make sure the results are what you want them to be. Another great alternative to robocopy is its non-ms twin, xxcopy.

    • Author
      Leos Marek 2 months ago

      I dont think that DST has any impact since NTFS timestamps are stored in UTC format. Of course, robocopy was created mainly for NTFS filesystem.

      • Samir 2 months ago

        It actually does between non-NTFS file systems/nas units. There is even the /DST switch to help with the issue.

        • Author
          Leos Marek 2 months ago

          I have never used robocopy on any other system than NTFS. Yes there is that switch which somehow filters the file selection. No idea how it works and the description in help is not much useful. 🙂

          • Samir 2 months ago

            A lot of sites have more details on the switches, but it is also hit and miss on how well it works depending on the source and destination.

            For one backup operation I had to make a ‘fall’ and ‘spring’ set since when dst kicked in, robocopy would copy everything again and not just the changed files.

            • Author
              Leos Marek 2 months ago

              For backups in general I prefer more sophisticsted tools, like free and great Cobian Backup with its own service, schedule, compressions etc..

              • Samir 2 months ago

                Backup programs can also add their own additional point of failure though if a restore has to be done through the backup program. This is why I’ve stuck with straight file copies for backups.

                • Author
                  Leos Marek 2 months ago

                  Thats the beauty of Cobian – it produce flat files copy or a simple zip archives that can be extracted by anything. Im preparing a post about Cobian, you will be able to read it soon if you like.

                  avatar
                  • Samir 2 months ago

                    Can’t wait to read about Cobian–seems like it’s pretty neat!

  2. Luis Maitret 2 months ago

    Similar to rclone?

    • Author
      Leos Marek 2 months ago

      Not using rclone but as I checked, its a similar tool. Robocopy is built-in in Windows and its not cross platform.

  3. mikgam 2 months ago

    It may be worth mentioning that the various Robocopy exit codes for successes and failures. 0 through 7 are all successes, 8 through 16 are failures.

    avatar
  4. Samir 2 months ago

    It might also be worth mentioning a few tricks for copying and comparison.

    For copying from one source to multiple destinations, using the ‘start’ command in a ‘for’ loop can start the copies simultaneously and allow the 100% cache hit to effectively double the copy speed since the original is basically only being read once and written twice. I’ve used this for up to 4 destinations simultaneously and it works best with similar destinations. Even when the destinations are not similar and the individual robocopy sessions become ‘off sync’ from each other, the latency of hitting the disk usually brings them back into alignment because one of the sessions has a cache hit while the other is still going to disk.

    For comparison, MS’s own windiff is a pretty good way to compare entire tree branches. However, if one needs to compare a single source to multiple destinations, winmerge does a better job since it has the ability to actually compare a single source to 2x destinations. And being a portable program that runs on the various windows platforms (just like windiff), it’s another handy tool that can check the work of a robocopy session for bit by bit accuracy.

    avatar
    • Author
      Leos Marek 2 months ago

      You are welcome to share such tricks with specific commands, readers would appriciate that.

      • Samir 2 months ago

        It’s pretty simple actually. If your source is T:\SUBDIR and destinations are \\10.10.10.1\VOLUME, \\10.10.10.2\VOLUME, \\10.10.10.2\VOLUME, then something like:[code]FOR %F IN (\\10.10.10.1\VOLUME \\10.10.10.2\VOLUME \\10.10.10.2\VOLUME) DO START ROBOCOPY T:\SUBDIR %F\SUBDIR /MIR /COPY:ALL /R:3 /W:3[code]I would also recommend testing to make sure your command is correct by using echo:
        [code]FOR %F IN (\\10.10.10.1\VOLUME \\10.10.10.2\VOLUME \\10.10.10.2\VOLUME) DO ECHO START ROBOCOPY T:\SUBDIR %F\SUBDIR /MIR /COPY:ALL /R:3 /W:3[code]

  5. Surender Kumar 2 months ago

    I use robocopy to copy bulk data on my eDiscovery servers and it still works great. You did a great job explaining its use cases. Thank you Leos.

  6. Job Cacka 2 months ago

    Thanks for posting this. I have used the MIR function of Robocopy to maintain a software distribution scheme so that all of the destinations end up with the same files as the source regularly. It “finds” corruption and corrects it nicely against a known good copy and helps force good update practices.

    • Samir 2 months ago

      How in the world is robocopy finding corruption and correcting it? afaik robocopy doesn’t have a built-in compare function.

      • Job Cacka 2 months ago

        The MIR function at a minimum compares timestamps. Unless there is a way to change a file without altering the timestamp (there maybe, I am unsure) then robocopy /MIR will ensure the source files overwrite the destination files if there is a change recorded in the OS.
        It probably wouldn’t repair hardware or driver level corruption, but it is great at fixing the corruption introduced by users.

        avatar
        • Samir 2 months ago

          I think I see what you’re saying. If someone alters the destination file, it would be replaced by the source to restore the ‘corruption’.

          • Job Cacka 2 months ago

            Yes, exactly. It makes even more sense when you understand the files we are overwriting are a mixture of executables, reports, dlls, and database edits.

            • Samir 2 months ago

              Whenever I have a need for something to be read only like this though, I’ll set set the permissions to read-only.

              avatar
              • Job Cacka 2 months ago

                Yes, we were told by the software vendor not to do that. So this is a bit of a work-around.

  7. Sarah_Maguir 2 weeks ago

    I got to know Robocopy years ago from a colleague in I.T.

    It didn’t take me long to realize I hate creating batch files so I did a google search for a Robocopy GUI

    and I found these There are these:

    SyncToy
    Gs Richcopy 360
    Syncback

    Some are free but the pay versions are reasonably priced.

    SyncToy is free and does not use Robocopy but it is pretty decent and was developed by a man from Microsoft.

    Gs Richcopy 360 and Syncback are fantastic and able to replace Robocopy , and are easy to use,full_featured, and can solve the issues that robocopy can’t handle.
    A quick Google search will yield results so I won’t post any URLs here.

    I am a GUI lover so I love these options.

    avatar
    • Michael Pietroforte 2 weeks ago

      Be careful with admitting that you are a GUI lover. This is almost like being an anti royalist these days. 😉

      avatar
  8. Oliv TheFrog 2 weeks ago

    Hello Sarah
    I too recognize that the GUI is very practical and useful … but only in certain cases: Simple and unitary tasks

    For all that is complex tasks – even simple but repetitive – nothing beats the command line and the script.

    you named 3 tools: SyncToy – Gs Richcopy 360 – Syncback
    Try copying millions of files, possibly without keeping the same tree structure at the target as at the source, and logging errors (the most frequent being missing NTFS permissions at the source), I wish you good luck with these tools.

    These 3 tools are undeniably practical for personal use only and limited in volume, not for professional use.

    • Sarah_Maguir 2 weeks ago

      As per my experince , You mean SyncToy , the Microsoft tool , but for this comment
      “Try copying millions of files, possibly without keeping the same tree structure at the target as at the source, and logging errors (the most frequent being missing NTFS permissions at the source) ” ,
      Gs Richcopy 360 and Syncback are created exactly for such cases.
      Backup/sync GUI tools like Gs Richcopy 360 and Syncback have a nice and simple GUI, able to copy to local drives, remote servers, LANs, WANs, and clouds, able to copy all the permissions types from source to destination, and also able to copy time stamps.
      and as I remember, there are options to throttle the connection speed to prevent bandwidth consumption, a feature to email you after the job is finished, an excellent task scheduler and it will never crash while transferring a large amount of data.
      there are also other differences between such a GUI tools and CLI tools, just try to search

  9. Samir 2 weeks ago

    I’ll take a command line over a GUI any day since it’s batchable. Plus I like the keyboard so anything that keeps me away from the mouse is my preferred platform.

    avatar

Leave a reply

Your email address will not be published.

*

© 4sysops 2006 - 2022

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account