Robocopy Cheat Sheet

Robocopy flags, filters and exit codes in one place: /MIR, /COPYALL, /MT, retry settings, logging switches and the exit code table that scripts get wrong.

Start here

Copy a tree, keep empty folders
/E
Make destination identical to source
/MIR
Keep NTFS permissions
/COPYALL
Show what would happen, change nothing
/L
Do not hang on a locked file
/R:2 /W:5
Success in a script
exit code below 8
/MIR deletes. Anything in the destination that is not in the source is removed, including a full destination when the source path is wrong or empty. Run the command once with /L before you run it for real.

Syntax

PartMeaning
robocopy source destination [file] [options]The order is fixed. Options always come last.
robocopy D:\data E:\backupFiles in the top folder only, no subfolders
robocopy D:\data E:\backup *.logOnly files matching the mask
robocopy "D:\my data" "E:\my backup" /EQuote any path with spaces, without a trailing backslash
robocopy \\SRV01\share E:\backup /EUNC source, no drive letter needed
Never end a quoted path with a backslash. "D:\data\" escapes the closing quote and robocopy reads the rest of the line as part of the path. Use "D:\data".

What to copy

FlagEffect
/SSubfolders, skipping empty ones
/ESubfolders including empty ones
/MIR/E plus /PURGE, a true mirror
/PURGEDelete destination items that no longer exist in the source
/MOVMove files, deleting them from the source
/MOVEMove files and folders, leaving the source empty
/CREATECreate the tree and zero-length files only
/LList only, copy nothing
/XXExclude extra files present only in the destination
/ISInclude files that are already identical

Attributes, ACLs and timestamps

FlagEffect
/COPY:DATThe default: data, attributes, timestamps
/COPY:DATSOUAdds security, owner and auditing
/COPYALLSame as /COPY:DATSOU
/SECSame as /COPY:DATS, data plus NTFS ACLs
/DCOPY:DATCopy folder timestamps too, not just file ones
/NOCOPYCopy nothing, used with /SECFIX or /TIMFIX
/SECFIXFix security on all files, including skipped ones
/TIMFIXFix timestamps on all files, including skipped ones
/A-:SHStrip the system and hidden attributes on the copy
/SLCopy symbolic links as links instead of their targets
A mirror is not a permissions copy. /MIR on its own carries /COPY:DAT, so the destination gets fresh inherited ACLs. Add /COPYALL when the permissions have to survive, and run it from an elevated session.

Filtering

FlagEffect
/XF patternExclude files by name or mask
/XD folderExclude folders, by name or full path
/XOExclude older files, do not overwrite a newer destination
/XNExclude newer files
/XCExclude changed files
/MAXAGE:nSkip files older than n days, or a date as YYYYMMDD
/MINAGE:nSkip files newer than n days
/MAX:n / /MIN:nSize limits in bytes
/IA:RASHCNETOInclude only files with these attributes
/XA:SHExclude system and hidden files
/XJExclude junctions, the flag that stops infinite loops
Use /XJ on any user profile or system folder. Legacy junctions such as Documents and Settings point back up the tree, and without /XJ robocopy follows them until the path limit stops it.

Speed, retries and network

FlagEffect
/MT:nMultithreaded copy, 1 to 128, default 8. The single biggest win on many small files.
/JUnbuffered I/O, use on very large files such as VHDX or backups
/ZRestartable mode, resumes a partial file after a network drop
/BBackup mode, uses the backup privilege to bypass ACLs
/ZBRestartable, falling back to backup mode on access denied
/R:nRetries per failed file. The default is 1,000,000.
/W:nSeconds between retries. The default is 30.
/IPG:nInter-packet gap in ms, throttles a WAN copy
/FFTTwo-second timestamp granularity, needed for many NAS targets
/DSTCompensate for one-hour daylight saving differences
/TBDWait for the share name to be defined
Always set /R and /W. With the defaults, one file held open by another process stops the job for roughly 347 days. /R:2 /W:5 is a sane starting point for anything scheduled.
/MT does not combine with everything. It cannot be used with /IPG or /EFSRAW, and its log lines arrive out of order because several threads write at once.

Logging

FlagEffect
/LOG:fileWrite to a log, overwriting it
/LOG+:fileAppend to an existing log
/UNILOG:fileSame, as Unicode
/TEEConsole and log at the same time
/NPNo percentage counter, essential in a log file
/NFL / /NDLNo file list / no folder list
/NJH / /NJSNo job header / no job summary
/VVerbose, shows skipped files as well
/TS / /FPShow file timestamps / full paths
/BYTESSizes in bytes instead of rounded units
/ETAEstimated finish time per file
The log-friendly set. /NP /NDL /TEE /LOG+:C:\logs\copy.log gives a readable file that stays small, while still listing every file that was actually copied.

Exit codes

CodeMeaning
0Nothing to do. Source and destination already match.
1Files were copied. This is success, not failure.
2Extra files or folders exist in the destination
3Files copied, extras present (1 + 2)
4Mismatched files or folders, a file where a folder was expected
5Files copied, mismatches present (1 + 4)
6Extras and mismatches, nothing copied (2 + 4)
7Copied, extras and mismatches (1 + 2 + 4)
8Some files could not be copied. Retry limit was reached.
16Fatal error. Nothing was copied, usually a bad path or no access.
CheckCommand
Batch, treat 8 and above as failureif %ERRORLEVEL% GEQ 8 exit /b 1
Batch, normalise success to 0if %ERRORLEVEL% LSS 8 exit /b 0
PowerShellif ($LASTEXITCODE -ge 8) { throw "Robocopy failed: $LASTEXITCODE" }
The codes are bit flags. 1 copied, 2 extras, 4 mismatches, 8 failures, 16 fatal. Anything below 8 is a normal run, which is why a scheduled task that treats a non-zero code as an error reports a healthy copy as broken.

Recipes

TaskCommand
Dry run before anything destructiverobocopy D:\data E:\backup /MIR /L /NP /NDL
Mirror a file server share with permissionsrobocopy \\SRV01\data E:\data /MIR /COPYALL /XJ /R:2 /W:5 /MT:16 /NP /LOG:C:\logs\data.log
Simple nightly backup, never deleterobocopy D:\data E:\backup /E /XO /R:2 /W:5 /NP /LOG+:C:\logs\backup.log
Move a folder to another volumerobocopy D:\old E:\new /MOVE /E /COPYALL /R:1 /W:1
Copy only what changed this weekrobocopy D:\data E:\delta /E /MAXAGE:7 /NP
Large files over a slow linkrobocopy \\SRV01\vm E:\vm /E /J /Z /R:5 /W:15 /IPG:20
Skip system noiserobocopy D:\ E:\ /E /XD "System Volume Information" "$RECYCLE.BIN" /XJ
Fix permissions without recopying datarobocopy D:\data E:\data /E /NOCOPY /SECFIX /COPYALL
Compare two trees, change nothingrobocopy D:\a E:\b /E /L /NJH /NJS /NDL /FP

FAQ

My scheduled task says the job failed, but everything copied. Why?
Task Scheduler treats any non-zero exit code as a failure, and a successful robocopy run returns 1. Wrap the call in a batch file or a PowerShell step that maps anything below 8 to 0.
Does /MIR copy NTFS permissions?
No. /MIR controls which files exist, not what travels with them. Add /COPYALL for data, attributes, timestamps, ACLs, owner and auditing, and run elevated, otherwise owner and auditing are silently dropped.
Why is the copy so slow on millions of small files?
Per-file overhead, not bandwidth. Raise /MT to 16 or 32, and cut the logging with /NP /NFL /NDL: writing a line per file to a network log can cost more than the copy itself.
Can robocopy handle paths longer than 260 characters?
Yes, it uses the Unicode long-path API by default, which is one of the main reasons it replaced xcopy. Only /256 turns that off.
How do I stop a mirror from deleting when the source is unavailable?
Test the source before the copy runs. In a script, check that a known file exists and exit early if it does not. Robocopy cannot tell an empty source from a missing one, and /MIR will faithfully mirror emptiness.