Robocopy: Reliable File Copying Utility

Complete guide to Robocopy (Robust File Copy) - Microsoft's command-line tool for reliable file replication, mirroring, and migration with resume capability.

2024-05-07 · 4 min read · 806 words · difficulty: Intermediate

#robocopy#Windows#IT Professional#File Copy#Migration#Backup#Command LineWindowsSystem AdministrationStorage

Table of contents

Robocopy (Robust File Copy) is Microsoft’s command-line directory replication tool, built into Windows since Vista/Server 2008. It replaces xcopy with superior reliability, resume capability, and advanced features for enterprise file operations.

Why Robocopy?

FeaturexcopyRobocopy
Resume on failure❌✅
Multi-threaded❌✅ (/MT)
Mirror directoriesLimited✅ (/MIR)
File attributesBasicFull preservation
Retry logicNoneConfigurable
LoggingMinimalComprehensive
UNC/Long pathsProblematicNative support
Bandwidth throttling❌✅ (/IPG)

Essential Syntax

robocopy <Source> <Destination> [<File>[ ...]] [<Options>]

Key concept: Robocopy works with directories, not individual files. Source and destination are folder paths.

Common Use Cases

1. Basic Copy (New/Changed Files Only)

robocopy "C:\Source" "D:\Backup" /E /Z /R:3 /W:5 /LOG:copy.log
  • /E - Copy subdirectories including empty
  • /Z - Restartable mode (resume on interruption)
  • /R:3 - Retry 3 times (default 1 million!)
  • /W:5 - Wait 5 seconds between retries
  • /LOG:file - Log to file

2. Mirror Directory (Exact Replica)

robocopy "C:\Source" "D:\Mirror" /MIR /Z /R:3 /W:5 /LOG:mirror.log
  • /MIR = /E + /PURGE (deletes destination files not in source)
  • Warning: Destination becomes exact clone, extra files deleted!

3. Incremental Backup (Daily)

robocopy "C:\Data" "\\Server\Backup\Data" /E /Z /R:2 /W:10 /XO /LOG+:daily.log
  • /XO - Exclude Older (skip if destination same/newer)
  • /LOG+ - Append to existing log

4. Multi-threaded High-Speed Copy

robocopy "C:\LargeFolder" "D:\Target" /E /Z /MT:16 /R:2 /W:5 /LOG:fast.log
  • /MT:n - Multi-threaded (1-128 threads, default 8)
  • Caution: High thread count can saturate disk/network

5. Network Copy with Throttling

robocopy "C:\Source" "\\NAS\Share" /E /Z /IPG:50 /R:5 /W:10 /LOG:netcopy.log
  • /IPG:n - Inter-Packet Gap (ms) for bandwidth throttling
  • Useful for WAN/VPN links

Complete Option Reference

Copy Options

OptionDescription
/SCopy subdirectories (non-empty)
/ECopy subdirectories (including empty)
/LEV:nCopy only top n levels
/ZRestartable mode
/BBackup mode (bypass ACLs, requires SeBackupPrivilege)
/ZBTry restartable, fallback to backup mode
/COPY:flagsWhat to copy (D=Data, A=Attributes, T=Timestamps, S=Security, O=Owner, U=Auditing)
/DCOPY:TCopy directory timestamps
/SECCopy security (equivalent to /COPY:DATS)
/COPYALLCopy all file info (DATSOU)

File Selection

OptionDescription
/ACopy only files with Archive attribute
/MCopy with Archive attribute + reset it
/IA:[RASHCNETO]Include only files with attributes
/XA:[RASHCNETO]Exclude files with attributes
/XF file [file...]Exclude Files (supports wildcards)
/XD dirs [dirs...]Exclude Directories
/XCExclude Changed files
/XNExclude Newer files
/XOExclude Older files
/XXExclude Extra files/dirs
/XLExclude Lonely files/dirs
/ISInclude Same files
/ITInclude Tweaked files
/MAX:nMaximum file size (bytes)
/MIN:nMinimum file size (bytes)
/MAXAGE:nMaximum file age (days)
/MINAGE:nMinimum file age (days)
/MAXLAD:nMaximum Last Access Date
/MINLAD:nMinimum Last Access Date

Retry & Logging

OptionDescription
/R:nRetry count (default 1,000,000)
/W:nWait between retries (default 30 sec)
/REGSave /R:/W: in registry as defaults
/TBDWait for share names To Be Defined
/LOG:fileOutput to log file (overwrite)
/LOG+:fileAppend to log file
/UNILOG:fileUnicode log (overwrite)
/UNILOG+:fileUnicode log (append)
/TEEOutput to console AND log
/NJHNo Job Header
/NJSNo Job Summary
/NPNo Progress display
/ETAShow ETA for files

Advanced

OptionDescription
/MIRMirror (/E + /PURGE)
/MOVEMove files (copy + delete source)
/MOVMove files (copy + delete source files only)
/PURGEDelete dest files not in source
/CREATECreate directory tree + zero-length files
/FATCreate 8.3 names
/FFTAssume FAT timestamps (2-sec granularity)
/MT[:n]Multi-threaded (n=1-128, default 8)
/IPG:nInter-Packet Gap (bandwidth throttle)
/JUnbuffered I/O (for huge files)

Real-World Scenarios

Server Migration

robocopy "\\OldServer\Data" "\\NewServer\Data" /E /COPYALL /DCOPY:T /ZB /MT:16 /R:3 /W:10 /LOG:migration.log /TEE

Daily Backup to Rotating Drives

@echo off
set DEST=Z:\Backup\%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%
robocopy "C:\Users" "%DEST%\Users" /E /Z /R:2 /W:5 /XO /LOG+:%DEST%\backup.log
robocopy "C:\Projects" "%DEST%\Projects" /E /Z /R:2 /W:5 /XO /LOG+:%DEST%\backup.log

Sync with Exclusions

robocopy "C:\Source" "D:\Sync" /MIR /XD ".git" "node_modules" "bin" "obj" /XF "*.tmp" "*.log" "Thumbs.db" /R:2 /W:5 /LOG:sync.log

Copy Only Permissions (ACLs)

robocopy "C:\Source" "D:\Dest" /E /COPY:S /IS /IT /LOG:perms.log
  • /COPY:S - Security only
  • /IS - Include Same (files that appear identical)
  • /IT - Include Tweaked (same data, different attributes)

Exit Codes (Bitwise)

CodeMeaning
0No files copied, no failures
1Files copied successfully
2Extra files/dirs detected
4Mismatched files/dirs
8Some files/dirs could not be copied
16Fatal error (no files copied)

Check in batch: if %errorlevel% geq 8 echo Error occurred

PowerShell Wrapper

function Invoke-Robocopy {
    param(
        [string]$Source,
        [string]$Dest,
        [string[]]$Options = @('/E','/Z','/R:3','/W:5','/LOG+:robocopy.log')
    )
    $args = @($Source, $Dest) + $Options
    & robocopy.exe @args
    $exitCode = $LASTEXITCODE
    if ($exitCode -ge 8) {
        Write-Error "Robocopy failed with exit code $exitCode"
    }
    return $exitCode
}

Best Practices

  1. Always test with /L (List only) first
  2. Use /LOG for audit trails
  3. Set reasonable /R and /W (not defaults)
  4. Use /MT judiciously - 8-16 for local, 4-8 for network
  5. Preserve timestamps with /DCOPY:T
  6. Mirror carefully - /MIR deletes!
  7. Test restores - Backup unverified is not a backup

Resources


MU
Madhusudan Upadhyay

ICT Support Executive · 11+ yrs · M365 · Intune · Windows Server & AD · Kathmandu. Work with me →

📬 New posts via RSS · /uses

Related reading