RoboCopy

When you’re managing Windows systems, especially in an enterprise or sysadmin environment, copying files reliably and preserving attributes, permissions, timestamps, and dealing with network glitches is critical. Enter RoboCopy (Robust File Copy) — a powerful command‑line utility built for such tasks.

In this guide, we’ll cover:

  • What RoboCopy is, and why it’s preferable to simpler copy tools
  • Core syntax and most useful switches
  • Real, practical examples (from basic to advanced)
  • Best practices, pitfalls, and troubleshooting
  • Suggested workflows and automation ideas

Let’s dig in.


What Is RoboCopy?

  • RoboCopy stands for “Robust File Copy.”
  • It is a Windows command‑line tool that supersedes older commands like COPY and XCOPY, providing far more control and resilience.
  • First introduced as part of the Windows NT Resource Kit, it is now built into modern Windows and Windows Server installations.
  • RoboCopy handles large data transfers, network interruptions, and attribute preservation better than legacy tools.

Because it’s so configurable, though, many users never exploit its full potential.


Basic Syntax & Parameters

The general syntax is:

robocopy <SourceDir> <DestinationDir> [<File(s)>] [<Options>]
  • <SourceDir> and <DestinationDir> are directory paths (with quotes if they contain spaces).
  • <File(s)> is optional — wildcard filters like *.txt can be used to limit which files are copied.
  • [Options] are switches that modify RoboCopy’s behavior (e.g. /E, /MIR, /COPYALL, /MT, etc.).

By default, if no filters or switches are given, RoboCopy will copy files (not directories) in the top level of the source to the destination.

Some important builtin behaviors and defaults:

  • RoboCopy will skip files in the destination that appear identical in size and timestamp (avoiding redundant copying).
  • It retries on failures (by default many times) unless told otherwise.
  • It outputs a summary result and sets an exit code (useful in scripts).
  • Some switches are incompatible with others (e.g. /MT doesn’t work with certain logging or progress options).

Key Switches & Options You Must Know

Here’s a curated list (not exhaustive) of the most useful RoboCopy switches and what they do:

SwitchWhat it does / Use case
/SCopy subdirectories, but skip empty ones
/ECopy subdirectories including empty ones
/LEV:nOnly copy the top n levels of the source tree
/ZUse “restartable” mode — can resume after interruption
/BBackup mode (override file permissions)
/ZBTry in restartable mode; if fails, use backup mode
/JUnbuffered I/O (good for very large files)
/COPY:flagsChoose what attributes to copy (D = Data, A = Attributes, T = Timestamps, S = Security / ACL, O = Owner, U = Auditing)
/COPYALLCopy all metadata (equivalent to /COPY:DATSOU)
/DCOPY:flagsSet what to copy for folders (data, attributes, timestamps)
/PURGEDelete destination files/dirs that no longer exist in source
/MIRMirror mode: same as /E + /PURGE
/MOVEMove files + directories — deletes source after copy
/MOVMove only files (not subdirectories)
/XFExclude certain files (wildcard)
/XDExclude certain directories
/MAX:n / /MIN:nExclude files larger / smaller than n bytes
/R:nNumber of retry attempts on failure (default is large)
/W:nWait time between retries (seconds)
/MT[:n]Multi-threaded copying with n threads (default is 8)
/IPG:nInter‑Packet Gap (useful to throttle bandwidth over network)
/LOG:filename / /LOG+:filenameWrite output (or append) to a log file
/LList only — simulate what would be done without actual copying
/NDL / /NFLNo directory list / no file list in output (cleaner logs)

Understanding combinations of these switches is key to mastering RoboCopy.


Real Examples (From Simple → Complex)

1. Basic file copy

robocopy "C:\SourceFolder" "D:\BackupFolder"

This copies files (not subfolders) from SourceFolder to BackupFolder.

2. Copy entire directory tree (all subfolders, including empty ones)

robocopy "C:\SourceFolder" "D:\BackupFolder" /E

3. Mirror a directory

robocopy "C:\SourceFolder" "D:\BackupFolder" /MIR

This ensures the destination is an exact match of source (removing files in destination that no longer exist in source).

4. Copy everything, preserving all metadata and ACLs

robocopy "C:\SourceFolder" "D:\BackupFolder" /E /COPYALL /DCOPY:T

5. Move files and directories instead of just copying

robocopy "C:\SourceFolder" "D:\BackupFolder" /MOVE /E

6. Exclude certain directories or file types

robocopy "C:\SourceFolder" "D:\BackupFolder" /E /XD "C:\SourceFolder\Temp" /XF *.tmp

This skips the Temp subfolder and all .tmp files.

7. Use multiple threads for speed (network or SSD)

robocopy "C:\SourceFolder" "\\Server\Backup" /E /MT:16 /Z

Here, 16 threads are used. Be cautious — high thread counts can saturate network or disk.

8. Limit by file age or size

robocopy "C:\SourceFolder" "D:\BackupFolder" /E /MAX:5000000

This copies only files ≤ ~5,000,000 bytes.

Or:

robocopy "C:\SourceFolder" "D:\BackupFolder" /E /MINAGE:30

Copy only files older than 30 days.

9. Log output cleanly

robocopy "C:\SourceFolder" "D:\BackupFolder" /E /LOG:"C:\Logs\robocopy_log.txt" /NFL /NDL

This writes a compact log without file-by-file detail.

10. Dry‑run / verify before actual run

robocopy "C:\SourceFolder" "D:\BackupFolder" /E /L

Using /L will list what would be copied / deleted, without doing anything. Use that before executing your real command — a safe practice.


Advanced Tips, Pitfalls & Best Practices

Use /L for testing

Always test complex commands with /L first to avoid accidental deletion or undesired behavior.

Beware of /MIR

While mirroring is powerful, combining with /PURGE means that if a file is deleted in the source, it will also be deleted in the destination. That’s not always desirable for backups.

Threading vs stability

While /MT can boost throughput, too many threads may overwhelm SMB, network links, or disks. Also, /MT disables some switches like /IPG and logging behavior nuances.

Handling locked files

If a file is in use and can’t be copied, you might get “access denied.” Use /ZB (fallback to backup mode) or schedule at times when files are less likely to be locked.

Path length issues

Older Windows versions or certain file systems may hit the 260-character path limit. RoboCopy can sometimes operate beyond this, but test it in your environment.

Use exit codes in scripts

RoboCopy returns a bitmask-style exit code. You can inspect %ERRORLEVEL% in batch files to decide if a copy succeeded fully, partially, or failed.

Logging & audits

Always direct logs (with /LOG or /LOG+) — don’t rely entirely on console output. Use /NFL /NDL to reduce verbosity when desired.

Scheduled / incremental backups

RoboCopy is ideal for nightly or periodic jobs. Use it with Task Scheduler, with /MIR or combinations targeting only changed files (via age, date, attributes).


Sample Use Cases / Scenarios

  1. Backup user profiles nightly
    Mirror from C:\Users to \\BackupServer\ProfilesBackup using /MIR + /Z + /R:3 /W:5 + logging.
  2. Migrate data to new server
    Use a “final sync” with /E /COPYALL /PURGE to ensure destination matches source exactly.
  3. Selective media copying
    Copy only .mp4 and .mov files from media servers to archive storage:
    robocopy "\\MediaSource" "D:\Archive" *.mp4 *.mov /S /R:2
  4. Clean up old logs
    Copy log files older than 90 days to a long-term archive:
    robocopy "C:\Logs" "D:\LogArchive" *.log /S /MAXAGE:90

Troubleshooting Common Issues

  • “Access denied” on some files — use /ZB, run as administrator, or temporarily close applications locking the file.
  • Too slow / performance issues — try /MT (multi-thread) or remove logging during transfer.
  • Files not copied — check filter rules (/XF, /MAX, /MIN, /LEV).
  • Unintended deletions — ensure /PURGE or /MIR aren’t misused in the wrong folder.
  • Path length / filename truncation — test with long path cases; adjust file structure if needed.
  • Exit code logic in scripts failing — interpret %ERRORLEVEL% correctly (bitmask flags).

Summary & Wrap-Up

RoboCopy is a Swiss‑Army knife for file copying, synchronization, and backup tasks on Windows. Once you get comfortable with its syntax and powerful switches, it can save hours of manual work and reduce error potential.

Key takeaways:

  • Always test with /L first
  • Use /COPY and /MT judiciously
  • Be cautious with /MIR in backup use
  • Log your operations and monitor exit codes
  • Automate using Task Scheduler and parameterize your scripts

With the examples and best practices above, you’re well on your way to mastering RoboCopy.

Leave a Reply

Your email address will not be published. Required fields are marked *