TechSide - Robocopy Command Line

Robocopy Command Line Tutorial

For: Windows

In an earlier blog entry, I showed you how to use Xcopy. This time I will show you an even more powerful Command(CMD) line utility called Robocopy, which is short for “Robust File Copy”. The Robocopy command line utility has even more advanced options (or switches), and was apparently released to replace Xcopy in newer Windows versions. I’m running Windows 10 and I have Xcopy commmand line and Robocopy commmand line, pre-installed on both of my Windows machines.

…[Robocopy Command Line] strengths include the ability to copy all NTFS file attributes and to mirror the contents of an entire folder hierarchy across local volumes or over a network.

Robocopy Command Line Syntax

robocopy Source Destination [File[ ...]] [Options]

To see the full list of options, type robocopy /? in the command prompt. The syntax of robocopy command line is different from other copy commands in that it accepts folder names as its source and destination — meaning you cannot use *.* or ?. However, certain files can be selected. If we want to copy a file named myinfo.txt from C:\draft” to “C:\published” then we can run the following command:

robocopy c:\draft c:\published myinfo.txt

Robocopy Command Line Key Features

robocopy command line syntaxGood Robocopy command line picture

Some key features of robocopy include:

  • Can recover from network interruptions and resume copying
  • A mirror (MIR) mode that deletes files from destination, which are no longer in the source
  • Multi-threaded Copying (/MT:8)
  • A command-line progress indicator
  • Copies more file attributes than Xcopy

Robocopy Command Line Examples:

robocopy "E:\American" "F:\Data\American" /log:LogFile1.txt /S /XO /NP /R:3 /W:5 /MT:8

The above robocopy command line example is copying files from E:\American to F:\Data\American using the following Robocopy Command Line switches:

  • /log:LogFile1.txt – outputting log file
  • /S – copies subdirectories (excludes empty subdirectories)
  • /XO – excludes older files
  • /NP – excludes progress of files copied
  • /R:3 – Retry 3 times (default value is 1,000,000 retries)
  • /W:5 – Wait 5 seconds between retries. Default is 30 seconds
  • /MT:8 – Multi-threaded copies (between 1 & 128)
  • You can find a list of all the Robocopy Command Line switches on Microsoft’s website: Robocopy
  • There are a lot more switches/options for Robocopy than Xcopy, and believe it or not, it’s much faster than “Dragging & Dropping” the files in Windows Explorer. Although Robocopy also comes in a GUI version, which can be downloaded and installed, getting used to the robocopy command line utility can be very beneficial because it’s easy, quick, and available on most Windows systems.

    Robocopy Command Line Error Codes:

    If you remember from my Xcopy entry a few days ago, It shows that Xcopy returns around 4 error codes we can make use of in a batch file. Well, the Robocopy command line can return around 10 different error codes:

    ROBOCOPY Errorcodes

    Error code Definition
    0 No Errors occurred & no copying done
    1 One or more files were copied successfully
    2 Some extra files or dirs detected. Nothing copied. Examine log.
    3 (2+1) Some files were copied. Additional files were present. No failure was encountered.
    4 Few mismatched files or dirs detected. Examine log
    5 (4+1) Some files were copied. Some files were mismatched. No failure was encountered.
    6 (4+2) Additional files and mismatched files exist. No files were copied and no failures were encountered. This means that the files already exist in the destination directory
    7 (4+2+1) Files were copied, a file mismatch was present, and additional files were present.
    8 Some files/dirs could not be copied. Check errors further.
    16 Serious error-No files copied. Syntax usage error or no privileges, etc.

    You can also create a batch file, similar to the xcopy batch file, to take care of the error codes returned by Robocopy. The following is a highly simplified Robocopy batch file:

    @echo off
    robocopy %1 %2
    IF %ERRORLEVEL% LSS 8 goto finish
    Echo Something failed & goto :eof
    Echo All done, no fatal errors.

    I named the batch file robust.bat. In the Command line, I ran:

    > robust "c:\mywebsite1" "o:\mywebsite2"

    These are just the basics of using the Robocopy command line utility. If you want more information regarding the robocopy parameters, or robocopy syntax, please visit: Microsoft Technet

    Tutorial: Learn to use Xcopy here
    Tutorial: Add any application to right-click menu
    Tutorial: Activate God mode in Windows

Leave a Reply

Your email address will not be published. Required fields are marked *