ROBOCOPY - CMD


Overview

ROBOCOPY (Robust File Copy) is a command-line utility in Windows for copying files and directories in a more advanced and reliable way than the standard copy command. This tool is designed for automatic and resilient file transfer, featuring abilities to handle network disruptions and file attribute settings. It’s widely used for backup processes, mirroring directories, and automating file replication tasks.

Syntax

The basic syntax for ROBOCOPY is as follows:

ROBOCOPY source destination [file [file]...] [options]
  • source: The path to the source directory.
  • destination: The path to the destination directory.
  • file: Optional. Specifies the file(s) to be copied. Use * or ? as wildcards to match multiple files.

Options/Flags

ROBOCOPY includes numerous options and flags that control its behavior:

  • /S: Copies subdirectories, but not empty ones.
  • /E: Copies subdirectories, including empty ones.
  • /Z: Copies files in restartable mode.
  • /ZB: Uses restartable mode; if access is denied, switches to backup mode.
  • /R:n: Specifies the number of retries on failed copies. Default is 1 million.
  • /W:n: Specifies the wait time between retries, in seconds. Default is 30.
  • /MT[:n]: Specifies the number of threads to use, default is 8. The /MT parameter can speed up copying significantly.
  • /XF file [file]...: Excludes specified files or patterns.
  • /XD dir [dir]...: Excludes directories specified.
  • /XC: Excludes changed files.
  • /XN: Excludes newer files.
  • /XO: Excludes older files.
  • /XX: Excludes extra files and directories.
  • /L: List only – does not copy, timestamp or delete any files.

Examples

  1. Basic Copying:

    ROBOCOPY C:\source C:\destination
    

    This command copies all files from the source to the destination directory.

  2. Copying with Subdirectories:

    ROBOCOPY C:\source C:\destination /S
    

    Copies source directory and all subdirectories except empty ones.

  3. Mirroring Directories:

    ROBOCOPY C:\source C:\destination /MIR
    

    Mirrors entire directory structure (equivalent to using /E and /PURGE).

  4. Using Multiple Threads:

    ROBOCOPY C:\source C:\destination /MT:16
    

    Uses 16 threads to copy files, improving performance on large copy jobs.

Common Issues

  • Access Denied: Can occur when copying files that require administrator privileges. Use /ZB to try backup mode.
  • Network Interruptions: Using /Z ensures that copying is paused and resumed in case of network issues.
  • Handling Long File Paths: ROBOCOPY can handle paths longer than 260 characters automatically.

Integration

ROBOCOPY can be integrated with CMD scripts for scheduled backups:

@echo off
SET source=C:\source
SET dest=E:\backup
ROBOCOPY %source% %dest% /MIR /R:5 /W:15 /LOG:E:\backup\log.txt

This script mirrors the directory annually and logs the output.

  • XCOPY: Another file copying tool, less robust but suitable for simpler tasks.
  • COPY: Basic file copying command in CMD.

For more detailed information, Microsoft’s official ROBOCOPY documentation provides extensive details and usage scenarios.