Introduction to Robocopy
ROBOCOPY (Robust File Copy) is a command-line tool built into Windows since Vista/Server 2008 that replaces XCOPY for serious file transfer work. Unlike COPY or XCOPY, Robocopy automatically retries a file copy if it hits a transient error such as a network share momentarily dropping, waits between retries, and can resume large transfers without restarting from scratch, which makes it the standard choice for backup scripts and scheduled synchronization jobs in production environments.
Cricket analogy: A team that keeps sending a runner back out after a dropped catch to try again, rather than abandoning the effort, mirrors Robocopy's automatic retry logic on a failed file copy.
Basic Syntax and Common Switches
The basic form is ROBOCOPY source destination [files] [options]. /E copies subdirectories including empty ones, /Z copies files in restartable mode so an interrupted large file transfer can resume instead of starting over, /R:n sets the number of retries on a failed file (default is one million, so scripts should always override it), and /W:n sets the seconds to wait between retries, so /R:3 /W:5 retries three times with a five-second pause is a common, script-friendly combination.
Cricket analogy: Setting a fixed number of overs to bowl at a batter in the nets rather than an unlimited session mirrors /R:n capping retry attempts to a sensible number instead of Robocopy's default of a million.
@echo off
REM Mirror a project folder to a backup drive, restartable, with bounded retries
ROBOCOPY C:\Projects D:\Backup\Projects /E /Z /R:3 /W:5 /LOG:D:\Backup\robocopy.log /MT:8
IF %ERRORLEVEL% GEQ 8 (
ECHO Robocopy reported a failure - check the log
) ELSE (
ECHO Backup completed successfully
)Mirroring vs Copying
/E copies all files and subfolders (including empty ones) from source to destination but leaves any extra files already present at the destination untouched. /MIR, by contrast, makes the destination an exact mirror of the source: it copies new and changed files just like /E, but it also deletes any file or folder at the destination that no longer exists in the source. This makes /MIR extremely powerful for true synchronization but also dangerous, since pointing /MIR at the wrong destination folder can silently wipe out files that were never meant to be removed.
Cricket analogy: Adding new players to a squad list without cutting anyone is like /E, while replacing the entire squad list so it exactly matches this season's official roster, cutting anyone not on it, is like /MIR.
ROBOCOPY /MIR will delete files and folders at the destination that are not present in the source, including files a user added directly at the destination after the last sync. Always verify the source and destination arguments are in the correct order and consider running with /L (list-only, no actual changes) first to preview what /MIR would delete.
Logging and Multithreading
The /LOG:filename switch writes Robocopy's output to a file instead of (or in addition to, with /LOG+:filename to append) the console, which is essential for unattended scheduled tasks where no one is watching the screen. /MT:n enables multithreaded copying with n threads (default 8 when /MT is used without a number), which dramatically speeds up copying many small files over a network. Robocopy also returns a non-standard exit code: unlike most commands where 0 means success and non-zero means failure, Robocopy uses a bitmask where 0-7 all indicate some form of success and 8 or higher indicates a real failure, so scripts must check IF %ERRORLEVEL% GEQ 8 rather than the usual IF %ERRORLEVEL% NEQ 0.
Cricket analogy: Splitting fielding drills across several coaches running simultaneously instead of one coach handling every player in turn mirrors /MT:8 running multiple copy threads in parallel.
Robocopy's exit codes are a bitmask, not the usual 0-means-success convention: 0 means no files copied (already in sync), 1 means files copied successfully, 2 means extra files/folders were detected, and these can combine (e.g. 3 = files copied plus extras detected). Any value of 8 or higher indicates at least one failure occurred, so the correct check in a script is IF %ERRORLEVEL% GEQ 8 rather than IF %ERRORLEVEL% NEQ 0.
- Robocopy automatically retries failed file copies, unlike COPY or XCOPY which fail immediately.
- Always override the default retry count with /R:n (e.g. /R:3) since the built-in default is one million retries.
- /E copies subfolders including empty ones but never deletes anything at the destination.
- /MIR makes the destination an exact mirror of the source, deleting extra files not present in the source.
- /LOG:filename redirects output to a file, which is essential for scheduled, unattended jobs.
- /MT:n enables multithreaded copying, greatly speeding up transfers of many small files.
- Robocopy's exit codes are a bitmask; check IF %ERRORLEVEL% GEQ 8 to detect real failures.
Practice what you learned
1. Why should scripts almost always specify /R:n explicitly when using Robocopy?
2. What is the key difference between /E and /MIR?
3. What does the /Z switch enable in Robocopy?
4. How should a batch script correctly check whether Robocopy failed?
5. What risk does ROBOCOPY /MIR carry that /E does not?
Was this page helpful?
You May Also Like
Copy, Move, and Delete Commands
Master COPY, XCOPY, MOVE, REN, and DEL to duplicate, relocate, and remove files and folders safely in Batch scripts.
File and Folder Operations
Learn how to navigate, create, and remove directories and inspect file attributes using core Windows Batch commands.
Batch File Redirection and Pipes
Understand stdin, stdout, and stderr redirection, piping commands together, and silencing output with NUL in Batch scripts.
Related Reading
Related Study Notes in Microsoft Technologies
Browse all study notesWindows 10 / UWP Development Study Notes
.NET · 30 topics
Microsoft TechnologiesMFC (Microsoft Foundation Classes) Study Notes
C++ · 30 topics
Microsoft TechnologiesSilverlight Study Notes
.NET · 30 topics
Microsoft TechnologiesXAML Study Notes
.NET · 30 topics
Microsoft TechnologiesWPF (Windows Presentation Foundation) Study Notes
.NET · 30 topics
Microsoft TechnologiesMVVM Design Pattern Study Notes
.NET · 30 topics