robocopy - Advanced Copy
When you want to back up or mirror folders, copy and xcopy can fall short.
ROBOCOPY (Robust File Copy) is a powerful file copy command built into Windows. It offers mirroring, incremental copying, retry control and logging for dependable copies.
This article covers robocopy from the basics to practical backup use, with sample code.
If you just want to copy a folder with everything in it, robocopy source destination /E is the basic form. To make the destination an exact copy, use /MIR. To judge the result, see the exit codes section.
robocopy syntax
ROBOCOPY source destination [file] [options]
| Element | Description |
|---|---|
| source | Source folder path |
| destination | Destination folder path |
| file | File name to copy (wildcards allowed; default is *.*) |
| options | Options that control the behavior |
Basic example
robocopy C:\Source D:\Backup
Main options
Copy options
| Option | Description |
|---|---|
/E | Copy subfolders, including empty ones |
/S | Copy subfolders (excluding empty ones) |
/MIR | Mirror (delete files that do not exist in the source) |
/PURGE | Delete destination files that no longer exist in the source |
/MOV | Move files (delete the originals after copying) |
/MOVE | Move files and folders |
/Z | Restartable mode (an interrupted copy can resume) |
/MT:n | Copy with n threads (1-128; 8 if omitted) |
/COPY:flags | Choose what to copy (D: data, A: attributes, T: timestamps, S: security) |
Selection options
| Option | Description |
|---|---|
/XF file | Exclude the specified files |
/XD folder | Exclude the specified folders |
/XO | Exclude older files (incremental copy) |
/MAXAGE:n | Exclude files older than n days |
/MINAGE:n | Exclude files newer than n days |
/MAX:n | Exclude files larger than n bytes |
/L | List what would happen without doing it |
Retry options
| Option | Description |
|---|---|
/R:n | Retries on failure (default: 1,000,000) |
/W:n | Wait between retries in seconds (default: 30) |
Log options
| Option | Description |
|---|---|
/LOG:file | Write the log to a file (overwrite) |
/LOG+:file | Append the log to a file |
/TEE | Output to both the console and log file |
/NP | Do not show the progress percentage |
/NFL | Do not log file names |
/NDL | Do not log directory names |
/V | Verbose output |
The default for /R is 1,000,000 retries. If a network connection drops during a copy, robocopy may keep retrying for a very long time. In real use, always set it explicitly, for example /R:3.
When a quoted path ends with \", the \ is treated as an escape for the " and the argument is not passed correctly. Do not write "C:\My Folder\"; write "C:\My Folder" without the trailing \.
Copy including subfolders (/E)
The /E option copies subfolders recursively, including empty ones.
robocopy C:\Source D:\Backup /E
Mirroring (/MIR)
The /MIR option reproduces the source folder structure exactly at the destination (it combines /E and /PURGE). Files that exist only in the destination are deleted.
robocopy C:\Source D:\Backup /MIR
/MIR deletes files and folders that exist only in the destination. If you get the destination path wrong, unintended files may be deleted. Always do a test run with /L the first time.
Test run (/L)
With /L, nothing is copied; robocopy only shows which files would be copied or deleted.
robocopy C:\Source D:\Backup /MIR /L
Always do a test run with /L before running a real backup, especially with /MIR or /PURGE, to avoid unexpected deletions.
Speed up copying (/MT)
/MT:n copies several files in parallel. It is especially effective when there are many small files.
robocopy C:\Source D:\Backup /E /MT:16
With /MT, several threads write to the log at the same time, so the order of log lines may be mixed.
Logging
How to record robocopy results in a log file.
robocopy C:\Source D:\Backup /E /LOG:"C:\Logs\backup.log" /TEE /NP
| Option | Description |
|---|---|
/LOG: | Specify the log file (overwrite) |
/TEE | Show output on the console and in the log |
/NP | Hide progress percentages (a cleaner log) |
To append to the log, use /LOG+:.
robocopy C:\Source D:\Backup /E /LOG+:"C:\Logs\backup.log" /TEE /NP
Exclude files and folders
Exclude files (/XF)
robocopy C:\Source D:\Backup /E /XF *.tmp *.log
Exclude folders (/XD)
robocopy C:\Source D:\Backup /E /XD "C:\Source\temp" "C:\Source\.git"
Practical example: automatic backup batch
A practical backup batch file using robocopy.
@echo off
setlocal
rem Backup settings
rem Note: the format of %DATE% depends on regional settings. This example assumes yyyy/mm/dd.
set SOURCE=C:\Users\user\Documents
set DEST=D:\Backup\Documents
set LOGFILE=D:\Backup\Logs\backup_%DATE:~0,4%%DATE:~5,2%%DATE:~8,2%.log
rem Create the log folder if it does not exist
if not exist "D:\Backup\Logs" mkdir "D:\Backup\Logs"
echo Starting backup...
echo Source: %SOURCE%
echo Destination: %DEST%
robocopy "%SOURCE%" "%DEST%" /MIR /R:3 /W:5 /LOG+:"%LOGFILE%" /TEE /NP /XD ".git" "node_modules"
rem Check the exit code
IF %ERRORLEVEL% LSS 8 (
echo Backup completed successfully.
) ELSE (
echo An error occurred. Please check the log.
)
endlocal
robocopy exit codes
robocopy returns its own exit codes. Unlike most commands, a non-zero code can still mean success. The code is a combination of these bits:
| Value | Meaning |
|---|---|
| 1 | One or more files were copied successfully |
| 2 | Extra files or folders were found in the destination |
| 4 | Mismatched files or folders were detected |
| 8 | Some files could not be copied (copy errors) |
| 16 | Fatal error (robocopy could not proceed, e.g. invalid path, no access, bad syntax) |
For example, exit code 3 means 1 (files copied) + 2 (extra files found). 0 means nothing needed to be copied (everything is identical).
Exit codes 0-7 are not failures; 8 or higher means an error. In a batch file, write IF %ERRORLEVEL% LSS 8. When /MIR deletes extra files, the code includes 2, which is not an error either.
robocopy vs. xcopy
| Feature | robocopy | xcopy |
|---|---|---|
| Mirroring | Yes (/MIR) | No |
| Retry control | Yes (/R, /W) | No |
| Logging | Yes (/LOG, /TEE) | No |
| Restartable mode | Yes (/Z) | Yes (/Z) |
| Multithreading | Yes (/MT) | No |
| Incremental copy | Yes (/XO) | Yes (/D) |
| Network resilience | Excellent | Basic |
| Exit codes | Detailed (0-16) | Basic (0-5) |
Microsoft has deprecated xcopy and recommends robocopy, so prefer robocopy for new batch files.
Summary
This article explained how to use the robocopy command.
robocopy is a powerful copy tool built into Windows. For backups in particular, combining mirroring, logging and retry control gives you a dependable setup.