Robocopy Cheat Sheet: Most Useful Options and a Backup Script Example

Category: Tips
Posted: September 6, 2011
Author: Antonio

I have already written about this small and wonderful backup tool that copies files over the network or from disk to disk.

You often need to recall what a particular switch does. Here is a short Robocopy cheat sheet with the options I use most. The full list is always available with robocopy /?.

Basic syntax

robocopy source destination [file [file]...] [options]

Copy options

  • /S copies subfolders, except empty ones.
  • /E copies subfolders, including empty ones.
  • /LEV:n copies only the top n levels of the source folder tree.
  • /Z copies files in restartable mode.
  • /B copies files in backup mode.
  • /ZB uses restartable mode, and if access is denied, switches to backup mode.
  • /COPY:flags sets what to copy (default is /COPY:DAT): D = data, A = attributes, T = timestamps, S = security (NTFS ACLs), O = owner info, U = auditing info.
  • /DCOPY:T copies folder timestamps.
  • /SEC copies files with security (same as /COPY:DATS).
  • /COPYALL copies all file info (same as /COPY:DATSOU).
  • /PURGE deletes destination files and folders that no longer exist in the source.
  • /MIR mirrors a directory tree (same as /E plus /PURGE).
  • /MOV moves files (deletes them from the source after copying).
  • /MOVE moves files and folders (deletes them from the source after copying).
  • /MON:n monitors the source and runs again after n changes.
  • /MOT:m monitors the source and runs again in m minutes if there are changes.
  • /RH:hhmm-hhmm sets the run hours, the time window when new copies may start.
  • /IPG:n sets an inter-packet gap (ms) to free up bandwidth on slow links.
  • /SL copies symbolic links themselves instead of their targets.

File selection options

  • /A copies only files with the Archive attribute set.
  • /M copies only files with the Archive attribute and resets it.
  • /XF file... excludes files matching the given names, paths, or wildcards.
  • /XD dir... excludes folders matching the given names or paths.
  • /XC, /XN, /XO exclude changed, newer, and older files.
  • /MAX:n and /MIN:n set the maximum and minimum file size in bytes.
  • /MAXAGE:n and /MINAGE:n set the maximum and minimum file age in days.
  • /XJ excludes junction points.
  • /FFT assumes FAT file times (2-second granularity), useful when copying to NAS devices.

Retry options

  • /R:n is the number of retries on failed copies (the default is 1 million).
  • /W:n is the wait time between retries (the default is 30 seconds).

Always set /R and /W in scripts, otherwise one locked file can make Robocopy wait for days.

Logging options

  • /L is list only: nothing is copied, deleted, or time-stamped. Use it to preview what /MIR will do.
  • /V produces verbose output, showing skipped files.
  • /TS, /FP include source timestamps and full path names in the output.
  • /NP turns off the progress percentage.
  • /NFL and /NDL turn off file and directory names in the log.
  • /LOG:file writes the status to a log file (overwriting it); /LOG+:file appends to it.
  • /UNILOG:file and /UNILOG+:file do the same in Unicode.
  • /TEE writes to both the console window and the log file.
  • /NJH and /NJS turn off the job header and the job summary.

Job options

  • /JOB:name takes parameters from a job file.
  • /SAVE:name saves the current parameters to a job file.
  • /QUIT quits after processing the command line (to view the parameters).

Example from a backup script

echo Start backup time: %DATE% %TIME% >> g:\BC\backup_time.txt
robocopy.exe S:\ g:\BC\MK\ /MIR /R:1 /W:1 /NP /TEE /LOG:g:\BC\00_sync.log
echo End backup time: %DATE% %TIME% >> g:\BC\backup_time.txt

This mirrors drive S: to g:\BC\MK\, retries a failed file once after one second, shows the output in the console, and writes a log. The script also records the start and end times in a text file.

Robocopy is included with Windows since Vista, so you don’t have to install anything on modern systems.

Related posts:

Leave a Comment

Copying content from this site is allowed only with a direct link to the source - https://nemcd.com