← All cheat sheets

ROBOCOPY

Authorized use only. Offensive reference for systems you own or are explicitly permitted to test. You are responsible for staying within the law.

Robust File Copy: Advanced file copying utility for Windows.
More reliable and feature-rich than xcopy.

BASIC SYNTAX#

robocopy source destination [files] [options]

BASIC USAGE#

robocopy C:\source D:\dest           # Copy folder contents
robocopy C:\source D:\dest *.txt     # Copy only .txt files
robocopy C:\source D:\dest file.txt  # Copy specific file
robocopy C:\source D:\dest /e        # Include empty subdirs
robocopy C:\source D:\dest /s        # Subdirs (not empty)

WHAT TO COPY#


    

FILE SELECTION#

/s                                   # Subdirectories (not empty)
/e                                   # Subdirectories (including empty)
/lev:n                               # Only top n levels
/maxage:n                            # Max file age (days or YYYYMMDD)
/minage:n                            # Min file age
/maxlad:n                            # Max last access date
/minlad:n                            # Min last access date
/max:n                               # Maximum file size (bytes)
/min:n                               # Minimum file size
/xf file [file]...                   # Exclude files
/xd dir [dir]...                     # Exclude directories
/a                                   # Only Archive attribute set
/m                                   # Like /a, but remove attribute
/ia:RASHCNETO                        # Include files with attributes
/xa:RASHCNETO                        # Exclude files with attributes

# Attributes: R=Read-only, A=Archive, S=System, H=Hidden
#            C=Compressed, N=Normal, E=Encrypted, T=Temporary, O=Offline

EXCLUDE EXAMPLES#

robocopy src dest /xf *.tmp *.log    # Exclude file types
robocopy src dest /xd temp cache     # Exclude directories
robocopy src dest /xf thumbs.db desktop.ini
robocopy src dest /xd .git node_modules

COPY OPTIONS#


    

ATTRIBUTES & SECURITY#

/copy:copyflag[s]                    # What to copy
  D = Data                           # File contents
  A = Attributes                     # File attributes
  T = Timestamps                     # Time stamps
  S = Security (NTFS ACLs)           # Permissions
  O = Owner info                     # File owner
  U = aUditing info                  # Audit settings

/copyall                             # Same as /copy:DATSOU
/sec                                 # Same as /copy:DATS
/dcopy:copyflag[s]                   # Directory copy (DAT)

robocopy src dest /copy:DAT          # Data, attributes, timestamps
robocopy src dest /copyall           # Everything including security
robocopy src dest /sec               # Copy security info

MIRRORING#

/mir                                 # Mirror directory tree
                                     # = /e /purge
/purge                               # Delete dest files not in source
/mov                                 # Move files (delete from source)
/move                                # Move files AND directories

RETRY & WAIT#

/r:n                                 # Number of retries (default 1M)
/w:n                                 # Wait time between retries (sec)
/reg                                 # Save /r:n and /w:n in registry
/tbd                                 # Wait for sharenames To Be Defined

# Recommended for network copies
robocopy src dest /r:3 /w:5          # 3 retries, 5 sec wait

PERFORMANCE#

/mt[:n]                              # Multi-threaded (default 8)
/j                                   # Unbuffered I/O
/nooffload                           # Don't use Windows Copy Offload
/compress                            # Request network compression
/efsraw                              # Copy encrypted files in RAW mode

robocopy src dest /e /mt:16          # 16 threads
robocopy src dest /e /mt /r:3 /w:5   # Fast with retry

OUTPUT & LOGGING#

/l                                   # List only (no copying)
/v                                   # Verbose output
/x                                   # Report extra files
/ts                                  # Include source timestamps
/fp                                  # Include full pathnames
/ns                                  # No file sizes
/nc                                  # No file classes
/nfl                                 # No file list
/ndl                                 # No directory list
/np                                  # No progress percentage
/eta                                 # Show ETA
/bytes                               # Print sizes as bytes
/unicode                             # Unicode status output

LOG FILES#

/log:file                            # Log to file (overwrite)
/log+:file                           # Log to file (append)
/unilog:file                         # Unicode log (overwrite)
/unilog+:file                        # Unicode log (append)
/tee                                 # Output to console AND log

robocopy src dest /e /log:copy.log /tee

JOB FILES#

/job:jobname                         # Load from job file
/save:jobname                        # Save to job file
/quit                                # Quit after processing

# Create job file
robocopy src dest /e /save:backup /quit

# Use job file
robocopy /job:backup

COMMON SCENARIOS#


    

BASIC BACKUP#

robocopy C:\Data D:\Backup /e /r:3 /w:5 /log:backup.log

MIRROR (SYNC)#

robocopy C:\Data D:\Backup /mir /r:3 /w:5

BACKUP WITH TIMESTAMPS#

robocopy C:\Data "D:\Backup\%date:~-4%%date:~4,2%%date:~7,2%" /e

NETWORK BACKUP#

robocopy C:\Data \\server\backup /e /z /r:3 /w:5 /mt:8

MOVE FILES#

robocopy C:\source D:\dest /move /e

COPY ONLY NEW/CHANGED#

robocopy src dest /e /xo             # Exclude older files

COPY LARGE FILES#

robocopy src dest /z /j /r:3 /w:5    # Restartable, unbuffered

COPY PERMISSIONS#

robocopy src dest /e /copyall

EXCLUDE TYPES#

robocopy src dest /e /xf *.tmp *.bak /xd temp .git

RESTARTABLE MODE#

/z                                   # Restartable mode (network)
/b                                   # Backup mode (needs SeBackup)
/zb                                  # Try /z, fallback to /b

EXIT CODES#

0                                    # No files copied (OK)
1                                    # Files copied successfully
2                                    # Extra files in destination
3                                    # Files copied + extra files
4                                    # Mismatched files/directories
5                                    # Copied + mismatched
6                                    # Extra + mismatched
7                                    # Copied + extra + mismatched
8+                                   # Errors occurred

# Check for errors in batch
if errorlevel 8 echo "Errors occurred"

SCHEDULING BACKUPS#

# Create scheduled task for daily backup
schtasks /create /tn "Daily Backup" /tr "robocopy C:\Data D:\Backup /mir /r:3 /w:5 /log:C:\logs\backup.log" /sc daily /st 03:00

FAT vs NTFS#

/fft                                 # Assume FAT file times (2-sec)
/dst                                 # Compensate for DST differences

MONITORING#

/mon:n                               # Monitor for n changes
/mot:m                               # Monitor every m minutes

# Continuous sync
robocopy src dest /mir /mon:1        # Copy when 1 change detected

PERMISSIONS ISSUES#

# When copying to network shares
robocopy src \\server\dest /e /copy:DAT
# (Don't copy security to avoid permission issues)

# Need backup privileges
robocopy src dest /e /b              # Backup mode

QUICK REFERENCE#

# Basic operations
robocopy src dest                    # Copy files
robocopy src dest /e                 # Include subdirs
robocopy src dest /mir               # Mirror (sync)
robocopy src dest /move              # Move files

# Common options
/e                                   # Subdirs including empty
/s                                   # Subdirs not empty
/purge                               # Delete extra in dest
/xf *.tmp                            # Exclude files
/xd temp                             # Exclude dirs
/r:3 /w:5                            # Retry settings
/mt:8                                # Multi-threaded
/log:file.log                        # Log file
/l                                   # List only (test)

# Full backup example
robocopy C:\Data D:\Backup /mir /r:3 /w:5 /mt:8 /log:backup.log /tee

# Network backup
robocopy C:\Data \\server\share /e /z /r:3 /w:5 /mt:8

# Copy only specific types
robocopy src dest *.doc *.pdf /e

# Exclude patterns
robocopy src dest /e /xf *.tmp /xd .git node_modules