Start here
Copy a tree, keep empty folders
/EMake destination identical to source
/MIRKeep NTFS permissions
/COPYALLShow what would happen, change nothing
/LDo not hang on a locked file
/R:2 /W:5Success 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
| Part | Meaning |
|---|---|
robocopy source destination [file] [options] | The order is fixed. Options always come last. |
robocopy D:\data E:\backup | Files in the top folder only, no subfolders |
robocopy D:\data E:\backup *.log | Only files matching the mask |
robocopy "D:\my data" "E:\my backup" /E | Quote any path with spaces, without a trailing backslash |
robocopy \\SRV01\share E:\backup /E | UNC 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
| Flag | Effect |
|---|---|
/S | Subfolders, skipping empty ones |
/E | Subfolders including empty ones |
/MIR | /E plus /PURGE, a true mirror |
/PURGE | Delete destination items that no longer exist in the source |
/MOV | Move files, deleting them from the source |
/MOVE | Move files and folders, leaving the source empty |
/CREATE | Create the tree and zero-length files only |
/L | List only, copy nothing |
/XX | Exclude extra files present only in the destination |
/IS | Include files that are already identical |
Attributes, ACLs and timestamps
| Flag | Effect |
|---|---|
/COPY:DAT | The default: data, attributes, timestamps |
/COPY:DATSOU | Adds security, owner and auditing |
/COPYALL | Same as /COPY:DATSOU |
/SEC | Same as /COPY:DATS, data plus NTFS ACLs |
/DCOPY:DAT | Copy folder timestamps too, not just file ones |
/NOCOPY | Copy nothing, used with /SECFIX or /TIMFIX |
/SECFIX | Fix security on all files, including skipped ones |
/TIMFIX | Fix timestamps on all files, including skipped ones |
/A-:SH | Strip the system and hidden attributes on the copy |
/SL | Copy 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
| Flag | Effect |
|---|---|
/XF pattern | Exclude files by name or mask |
/XD folder | Exclude folders, by name or full path |
/XO | Exclude older files, do not overwrite a newer destination |
/XN | Exclude newer files |
/XC | Exclude changed files |
/MAXAGE:n | Skip files older than n days, or a date as YYYYMMDD |
/MINAGE:n | Skip files newer than n days |
/MAX:n / /MIN:n | Size limits in bytes |
/IA:RASHCNETO | Include only files with these attributes |
/XA:SH | Exclude system and hidden files |
/XJ | Exclude 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
| Flag | Effect |
|---|---|
/MT:n | Multithreaded copy, 1 to 128, default 8. The single biggest win on many small files. |
/J | Unbuffered I/O, use on very large files such as VHDX or backups |
/Z | Restartable mode, resumes a partial file after a network drop |
/B | Backup mode, uses the backup privilege to bypass ACLs |
/ZB | Restartable, falling back to backup mode on access denied |
/R:n | Retries per failed file. The default is 1,000,000. |
/W:n | Seconds between retries. The default is 30. |
/IPG:n | Inter-packet gap in ms, throttles a WAN copy |
/FFT | Two-second timestamp granularity, needed for many NAS targets |
/DST | Compensate for one-hour daylight saving differences |
/TBD | Wait 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
| Flag | Effect |
|---|---|
/LOG:file | Write to a log, overwriting it |
/LOG+:file | Append to an existing log |
/UNILOG:file | Same, as Unicode |
/TEE | Console and log at the same time |
/NP | No percentage counter, essential in a log file |
/NFL / /NDL | No file list / no folder list |
/NJH / /NJS | No job header / no job summary |
/V | Verbose, shows skipped files as well |
/TS / /FP | Show file timestamps / full paths |
/BYTES | Sizes in bytes instead of rounded units |
/ETA | Estimated 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
| Code | Meaning |
|---|---|
0 | Nothing to do. Source and destination already match. |
1 | Files were copied. This is success, not failure. |
2 | Extra files or folders exist in the destination |
3 | Files copied, extras present (1 + 2) |
4 | Mismatched files or folders, a file where a folder was expected |
5 | Files copied, mismatches present (1 + 4) |
6 | Extras and mismatches, nothing copied (2 + 4) |
7 | Copied, extras and mismatches (1 + 2 + 4) |
8 | Some files could not be copied. Retry limit was reached. |
16 | Fatal error. Nothing was copied, usually a bad path or no access. |
| Check | Command |
|---|---|
| Batch, treat 8 and above as failure | if %ERRORLEVEL% GEQ 8 exit /b 1 |
| Batch, normalise success to 0 | if %ERRORLEVEL% LSS 8 exit /b 0 |
| PowerShell | if ($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
| Task | Command |
|---|---|
| Dry run before anything destructive | robocopy D:\data E:\backup /MIR /L /NP /NDL |
| Mirror a file server share with permissions | robocopy \\SRV01\data E:\data /MIR /COPYALL /XJ /R:2 /W:5 /MT:16 /NP /LOG:C:\logs\data.log |
| Simple nightly backup, never delete | robocopy D:\data E:\backup /E /XO /R:2 /W:5 /NP /LOG+:C:\logs\backup.log |
| Move a folder to another volume | robocopy D:\old E:\new /MOVE /E /COPYALL /R:1 /W:1 |
| Copy only what changed this week | robocopy D:\data E:\delta /E /MAXAGE:7 /NP |
| Large files over a slow link | robocopy \\SRV01\vm E:\vm /E /J /Z /R:5 /W:15 /IPG:20 |
| Skip system noise | robocopy D:\ E:\ /E /XD "System Volume Information" "$RECYCLE.BIN" /XJ |
| Fix permissions without recopying data | robocopy D:\data E:\data /E /NOCOPY /SECFIX /COPYALL |
| Compare two trees, change nothing | robocopy 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.