Workflow Tools Control Yard

Home / Windows

Robocopy command builder

Choose what the copy should do and get a robocopy command with quoting, retries and logging filled in. Further down: paste a command to have it explained, an exit code decoder for scripts and RMM jobs, and every switch.

Command

robocopy <source> <destination>
Folders
Subfolders
After copying
File details to copy
Copy mode
Retries
Filters
Logging and output

Explain a robocopy command

Paste a robocopy command to see what each part does and what to check before it runs.

Robocopy exit codes

Robocopy does not use 0 for success and everything else for failure. Its exit code is a sum of flags, and any value below 8 means no file failed to copy. A scheduler or RMM tool that treats every non-zero code as an error reports a normal copy, exit code 1, as failed.

FlagMeaning when set
1One or more files were copied.
2Extra files or folders were found in the destination that are not in the source. /MIR and /PURGE delete them; otherwise they are left alone.
4Mismatched files or folders were found, such as a file in the source where the destination has a folder of the same name. These need a manual look.
8Some files or folders could not be copied. Copy errors occurred or the retry limit was reached.
16Serious error. Robocopy copied nothing: the command line was wrong, or it had no access to the source or destination.

An exit code of 0 means nothing was copied and nothing failed: source and destination already match. Add the flags for other values, so 3 is "files copied, extra files present" and 9 is "some files copied, some failed".

Use the exit code in a script

Batch file or RMM command step:

robocopy "C:\Data" "\\server\share\Data" /E /R:2 /W:5 /NP /LOG:"C:\Logs\copy.log"
if %ERRORLEVEL% GEQ 8 exit /b %ERRORLEVEL%
exit /b 0

PowerShell:

robocopy 'C:\Data' '\\server\share\Data' /E /R:2 /W:5 /NP /LOG:'C:\Logs\copy.log'
if ($LASTEXITCODE -ge 8) { exit $LASTEXITCODE }
exit 0

Robocopy switches

Every switch of robocopy, in the groups its own help uses. The explainer above reads this table.

SwitchWhat it does
Copying
/SCopy subfolders, skipping empty ones.
/ECopy subfolders, including empty ones.
/LEV:nCopy only the top n levels of the source tree.
/ZRestartable mode: an interrupted file resumes where it stopped. Slower than a normal copy.
/BBackup mode: read and write files regardless of their permissions, using the backup and restore privileges.
/ZBRestartable mode that switches to backup mode when access is denied.
/JUnbuffered I/O. Suits very large files.
/EFSRAWCopy EFS-encrypted files in raw mode: the encrypted data is copied as it is.
/COPY:flagsWhich file properties to copy: D data, A attributes, T timestamps, X skip alternate data streams, S NTFS permissions, O owner, U auditing. The default is DAT.
/DCOPY:flagsWhich folder properties to copy: D data, A attributes, T timestamps, E extended attributes, X skip alternate data streams. The default is DA.
/SECSame as /COPY:DATS: also copy NTFS permissions.
/COPYALLSame as /COPY:DATSOU: copy every file property.
/NOCOPYCopy no file properties. Useful together with /PURGE.
/SECFIXFix the permissions on all files, the skipped ones too.
/TIMFIXFix the timestamps on all files, the skipped ones too.
/PURGEDelete files and folders in the destination that are no longer in the source.
/MIRMirror the tree: /E plus /PURGE. Deletes anything in the destination that is not in the source.
/MOVMove files: delete each file from the source after it is copied.
/MOVEMove files and folders: delete both from the source after copying.
/A+:attributesAdd the named attributes to the copied files: R read only, A archive, S system, H hidden, C compressed, N not content indexed, E encrypted, T temporary.
/A-:attributesRemove the named attributes from the copied files. The same letters, and O for offline.
/CREATECreate the folder tree and zero-length files only.
/FATCreate the destination files with 8.3 FAT names only.
/256Turn off support for paths longer than 256 characters.
/MON:nWatch the source and run again when more than n changes are seen.
/MOT:mWatch the source and run again in m minutes if something changed.
/RH:hhmm-hhmmThe hours of the day during which new copies may start.
/PFCheck those hours for each file, not once per pass.
/IPG:nWait n milliseconds between packets, to leave bandwidth free on slow lines.
/SJCopy junctions as junctions, not what they point to.
/SLCopy symbolic links as links, not what they point to.
/MT:nCopy n files at a time, 1 to 128, default 8. Cannot be combined with /IPG or /EFSRAW.
/NODCOPYCopy no folder properties. Without it, /DCOPY:DA applies.
/NOOFFLOADCopy without the Windows copy offload mechanism.
/COMPRESSAsk for network compression during the transfer, where it is available.
/SPARSE:y|nKeep (y) or drop (n) the sparse state of files. Keeps it when no value is given.
/NOCLONEDo not try block cloning to speed up the copy.
Throttling
/IOMAXSIZE:n[kmg]The largest I/O size for one read or write cycle, in kilobytes, megabytes or gigabytes.
/IORATE:n[kmg]The I/O rate to hold to, per second.
/THRESHOLD:n[kmg]The file size from which throttling applies. Smaller files are not throttled.
Choosing files
/ACopy only files that have the Archive attribute set.
/MCopy only files that have the Archive attribute set, and clear it.
/IA:attributesInclude only files that have any of the named attributes: R read only, A archive, S system, H hidden, C compressed, N not content indexed, E encrypted, T temporary, O offline.
/XA:attributesExclude files that have any of the named attributes. The same letters.
/XF name ...Exclude files by name, path or wildcard.
/XD name ...Exclude folders by name or path.
/XCExclude changed files: the same timestamp, a different size.
/XNExclude source files that are newer than the copy in the destination.
/XOSkip source files that are older than the copy already in the destination.
/XXExclude extra files and folders: in the destination, not in the source. Nothing is deleted from the destination.
/XLExclude lonely files and folders: in the source, not in the destination. No new file reaches the destination.
/IMInclude modified files: the same but for their change time.
/ISInclude same files: identical in name, size, times and attributes. They are copied again.
/ITInclude tweaked files: the same name, size and times, different attributes.
/MAX:nExclude files larger than n bytes.
/MIN:nExclude files smaller than n bytes.
/MAXAGE:nExclude files last modified more than n days ago, or before a date. An n below 1900 counts days; a larger one is a date, YYYYMMDD.
/MINAGE:nExclude files last modified less than n days ago, or after a date. Days or a date, as for /MAXAGE.
/MAXLAD:nExclude files not used since n. Days or a date, as for /MAXAGE.
/MINLAD:nExclude files used since n. Days or a date, as for /MAXAGE.
/XJSkip junction points. Without it, a junction that points back into the tree, as in user profiles, can make the copy loop.
/XJDSkip junction points to folders.
/XJFSkip junction points to files.
/FFTCompare file times with 2-second precision, for targets that round timestamps.
/DSTAllow for one-hour daylight saving time differences.
/IF name ...Include only the files named after it.
Retries
/R:nRetries for each failed file. The default is 1,000,000.
/W:nSeconds to wait between retries. The default is 30.
/REGSave /R and /W as the defaults in the registry.
/TBDWait for share names to be defined (retry on error 67).
/LFSM:n[kmg]Low free space mode: pause when a copy would take the destination below a floor of free space. The floor is 10% of the volume when no size is given. Cannot be combined with /MT or /EFSRAW.
Logging and output
/LList only. Shows what would be copied or deleted without doing it.
/XReport all extra files, not only the selected ones.
/VVerbose output that also lists skipped files.
/TSShow source file timestamps in the output.
/FPShow the full path of each file in the output.
/BYTESShow sizes in bytes.
/NSLeave file sizes out of the output.
/NCLeave file classes out of the output.
/NFLLeave file names out of the output.
/NDLLeave folder names out of the output.
/NPLeave out the percentage progress.
/ETAShow the estimated time of arrival of the files being copied.
/LOG:fileWrite the output to a log file, replacing the file.
/LOG+:fileAppend the output to a log file.
/UNILOG:fileWrite the output to a log file as Unicode text, replacing the file.
/UNILOG+:fileAppend the output to a log file as Unicode text.
/TEEWrite to the console as well as the log file.
/NJHLeave out the job header.
/NJSLeave out the job summary.
/UNICODEShow the output as Unicode text.
Job files
/JOB:nameTake the parameters from the named job file.
/SAVE:nameSave the parameters to the named job file.
/QUITStop after reading the command line, to show the parameters.
/NOSDNo source folder is given.
/NODDNo destination folder is given.

More Windows tools