cheat-sheets

Robocopy

robocopy (Robust File Copy) is the built-in Windows tool for copying and mirroring folder trees, with retries, logging, multithreading and permission handling. Run it from cmd or PowerShell (PowerShell Cheat-Sheet). On Linux the equivalent is rsync; see Linux Basics.

Syntax: robocopy <SOURCE> <DEST> [FILES] [OPTIONS]. It copies the contents of the source folder into the destination folder.

Documentation: robocopy reference

Common Commands #

COMMANDDESCRIPTION
robocopy C:\src D:\dstCopy files in the top folder only
robocopy C:\src D:\dst /ECopy subfolders including empty ones
robocopy C:\src D:\dst /SCopy subfolders, skip empty ones
robocopy C:\src D:\dst *.docx *.xlsx /SOnly matching files
robocopy C:\src D:\dst /E /XF *.tmp *.logExclude files
robocopy C:\src D:\dst /E /XD node_modules .gitExclude directories
robocopy C:\src D:\dst /E /MT:16Multithreaded copy with 16 threads (default 8)
robocopy C:\src D:\dst /E /COPYALLCopy data, attributes, timestamps, ACLs, owner and auditing info (needs admin)
robocopy C:\src D:\dst /E /DCOPY:DATAlso keep directory timestamps
robocopy C:\src D:\dst /E /LDry run: list what would happen, copy nothing
robocopy C:\src D:\dst /E /ZRestartable mode (survives network drops, slower)
robocopy C:\src D:\dst /E /ZBRestartable, fall back to backup mode if access is denied
robocopy C:\src D:\dst /E /JUnbuffered I/O, faster for very large files
robocopy C:\src D:\dst /E /SLCopy symbolic links as links, not their targets
robocopy C:\src D:\dst /E /XJSkip junction points (avoids loops such as profile folders)
robocopy \\<SERVER>\share D:\dst /E /COPY:DAT /R:2 /W:5Copy from a network share with sensible retries

Copy vs Mirror #

MODECOMMANDEFFECT
Copy (safe)robocopy src dst /EAdds new and changed files; never deletes in the destination
Moverobocopy src dst /E /MOVECopies, then deletes files and folders from the source (/MOV moves files only)
Mirrorrobocopy src dst /MIRMakes dst identical to src, deleting anything in dst that is not in src
Purgerobocopy src dst /E /PURGEDeletes destination files and folders that no longer exist in the source (/MIR is /E /PURGE)

/MIR is /E /PURGE. A wrong destination, or an empty or unavailable source, can wipe the destination. Always do a dry run first and double check the argument order:

powershell
robocopy C:\data E:\backup\data /MIR /L /NP /NJH /NJS   # preview only
robocopy C:\data E:\backup\data /MIR /R:2 /W:5 /XA:SH /LOG+:C:\Logs\mirror.log

A mirror is not a backup: deletions and corruption replicate too, so keep versioned backups as well. Never mirror onto the root of a drive unless you mean it: /MIR can delete protected System Volume Information content, so exclude it with /XD "System Volume Information" '$RECYCLE.BIN'.

Retries and Timing #

OPTIONDESCRIPTION
/R:<N>Retries on a failed copy (default is 1,000,000, which can look like a hang)
/W:<S>Seconds between retries (default 30)
/REGSave /R and /W as defaults in the registry
/TBDWait for share names to be defined (retry error 67)
/MON:<N>Monitor the source and rerun after N changes
/MOT:<M>Monitor and rerun every M minutes
/RH:hhmm-hhmmRun only within a time window (run hours)
/IPG:<MS>Inter-packet gap in ms to throttle bandwidth on slow links
/MAXAGE:<N> / /MINAGE:<N>Exclude files older or newer than N days (or a YYYYMMDD date)
/MAX:<BYTES> / /MIN:<BYTES>Exclude files larger or smaller than a size
/XOExclude older files (do not overwrite newer destination files)
/XC / /XNExclude changed / newer files
/FFTAssume FAT file times (2 second granularity), useful for NAS and Linux shares
/COMPRESSRequest SMB compression (Windows 11 and Server 2022, SMB 3.1.1)

Always set /R and /W for unattended jobs, for example /R:3 /W:10.

What Gets Copied #

/COPY:<flags> picks the file info (default DAT): D data, A attributes, T timestamps, S NTFS security (ACLs), O owner, U auditing.

OPTIONDESCRIPTION
/COPY:DATDefault: data, attributes, timestamps
/COPY:DATSAdd ACLs
/COPYALLSame as /COPY:DATSOU
/SECSame as /COPY:DATS
/DCOPY:DATCopy directory data, attributes and timestamps (T is important for folder dates)
/NOCOPYCopy no file info (use with /PURGE)
/A+:R / /A-:RAdd or remove the read-only attribute on copied files
/XA:SHExclude system and hidden files
/ITInclude tweaked files (same size and time, different attributes)
/SECFIXFix ACLs on all files, even skipped ones
/TIMFIXFix timestamps on all files, even skipped ones

For a full file server migration with permissions: robocopy <SRC> <DST> /MIR /COPYALL /DCOPY:DAT /R:3 /W:5 /MT:16 /XJ /LOG:C:\Logs\migrate.log /TEE /NP.

Logging and Output #

OPTIONDESCRIPTION
/LOG:<FILE>Write output to a file (overwrites)
/LOG+:<FILE>Append to an existing log
/UNILOG:<FILE> / /UNILOG+:<FILE>Unicode log (use when file names contain non-ASCII characters)
/TEEAlso print to the console
/NPNo progress percentage (keeps logs clean)
/NFL / /NDLNo file names / no directory names in the log
/NJH / /NJSNo job header / no job summary
/VVerbose, show skipped files
/FPFull path names in the output
/BYTESPrint sizes in bytes
/ETAShow estimated time of arrival
/LList only (no changes)

Exit Codes #

The exit code is a bit field, so it is a sum. Codes 0 to 7 are success or partial success; 8 and above indicate failures.

CODEMEANING
0Nothing copied, no differences
1Files copied successfully
2Extra files or directories in the destination that are not in the source
31 + 2: files copied and extras detected
4Mismatched files or directories (same name, different type)
51 + 4
62 + 4
71 + 2 + 4: files copied, extras and mismatches
8Some files or directories could not be copied (failures after retries)
16Fatal error: nothing was copied (bad path, bad syntax, access denied)

Add them: 9 means files copied plus failures. In scripts treat -ge 8 as an error:

powershell
robocopy C:\src D:\dst /E /R:2 /W:5 /LOG:C:\Logs\job.log /NP
if ($LASTEXITCODE -ge 8) { Write-Error "Robocopy failed with code $LASTEXITCODE"; exit $LASTEXITCODE }
else { $global:LASTEXITCODE = 0 }   # reset so schedulers and CI do not see 1 to 7 as failure

In batch files use if %ERRORLEVEL% GEQ 8 (echo failed).

Scheduled Backup Example #

powershell
$roboArgs = '"D:\Data" "\\<NAS>\backup\Data" /MIR /R:3 /W:10 /MT:8 /XJ /NP /LOG+:"C:\Logs\backup.log"'
$action  = New-ScheduledTaskAction -Execute "robocopy.exe" -Argument $roboArgs
$trigger = New-ScheduledTaskTrigger -Daily -At 2am
Register-ScheduledTask -TaskName "NightlyRobocopy" -Action $action -Trigger $trigger -User "SYSTEM" -RunLevel Highest

Scheduled tasks are covered in PowerShell Cheat-Sheet.

Troubleshooting #

SYMPTOMFIX
Looks hungToo many retries on a locked file: set /R:2 /W:5
ERROR 5 (0x00000005) Access is deniedRun elevated, use /ZB or /B, check NTFS and share permissions
ERROR 32 file in useClose the program or use VSS snapshots for open files; robocopy has no built-in VSS support
ERROR 123 / ERROR 3 path problemsCheck quoting and trailing backslash: "C:\path with spaces\" ends the quote early; write "C:\path with spaces\\" or drop the trailing \
Copies everything every timeTime-stamp granularity: add /FFT for NAS or FAT; use /DCOPY:T
Long paths (over 260 characters)Robocopy handles long paths; use \\?\ prefix if needed and enable long paths in Windows
Source and destination swappedAlways test with /L first; sources go first

Alternatives #

TOOLUSE
xcopyLegacy, fewer options, no mirror
Copy-Item -RecurseSimple PowerShell copies, no retries or mirror
rsync (via WSL Cheat-Sheet)Delta transfers over SSH (OpenSSH Cheat-Sheet)
robocopyLarge trees, permissions, mirrors, resumable network copies

Not Verified #

Options and exit codes were written from the Microsoft documentation and not executed. /COMPRESS needs recent Windows and SMB versions.

Search all sheets

Tip: / or Ctrl/Cmd+K to open, Esc to close.