Skip to main content

Using Robocopy for Advanced File Copying in a Batch Script

When it comes to copying files and directories in a Windows batch script, the standard COPY and XCOPY commands are adequate for simple tasks. However, for any serious, reliable, or complex file transfer, the definitive tool is Robocopy.exe (Robust File Copy). It is a powerful, built-in utility designed for resilience and control, making it the professional standard for backups, deployments, and synchronization.

This guide will introduce you to the most important features of Robocopy. You will learn its basic syntax and the key switches that make it superior to its predecessors, including its ability to mirror directories, handle locked files, log its activity, and filter files.

Why Use Robocopy Instead of XCOPY?

Robocopy is superior to XCOPY in almost every way for scripting:

  • Resilience: It can automatically retry copying a file if it's locked by another program. XCOPY would just fail.
  • Efficiency: It can copy only new or changed files, making it perfect for daily backups. XCOPY is less intelligent.
  • Mirroring: It can delete files in the destination that no longer exist in the source (/MIR), perfectly synchronizing two folders.
  • Logging: It has built-in, detailed logging capabilities.
  • Attributes: It can preserve all file attributes, timestamps, security permissions, and owner information.

The Core Syntax

The basic Robocopy command is simple, specifying a source and a destination.

ROBOCOPY "Source_Folder" "Destination_Folder" [files_to_copy] [options]
  • "Source_Folder": The directory where the files are coming from.
  • "Destination_Folder": The directory where the files are going.
  • [files_to_copy] (Optional): A file specifier, like *.log. If omitted, it defaults to *.* (all files).
  • [options]: The command-line switches that control its behavior. This is where the real power lies.

Key Feature 1: Mirroring a Directory (/MIR)

This is one of Robocopy's most powerful features. The /MIR (MIRror) switch makes the destination an exact replica of the source.

ROBOCOPY "C:\MyData" "D:\Backup\MyData" /MIR

This single switch will:

  1. Copy all new and updated files from the source to the destination.
  2. Copy all subdirectories (even empty ones).
  3. Delete any files in the destination that have been deleted from the source.

WARNING: The /MIR switch is destructive to the destination. A typo in your paths could cause catastrophic data loss. Always double-check your command.

Key Feature 2: Resilience and Retries (/R and /W)

This feature makes Robocopy perfect for unreliable networks or for copying files that might be temporarily in use.

  • /R:<N>: Specifies the number of Retries on a failed copy.
  • /W:<N>: Specifies the Wait time in seconds between each retry.

Best Practice Syntax: ... /R:3 /W:10 This tells Robocopy to try copying a locked file up to 3 times, waiting 10 seconds between each attempt, before finally giving up and moving to the next file.

Key Feature 3: Filtering Files and Folders (/XF, /XD)

You often don't want to copy everything. Robocopy provides powerful exclusion switches.

  • /XF <File>: eXclude File. You can use wildcards (e.g., *.tmp, ~$*).
  • /XD <Dir>: eXclude Directory. You can use wildcards (e.g., obj, node_modules).

This command mirrors a project folder but excludes temporary files and the build output directory.

ROBOCOPY "C:\MyProject" "E:\Backup\MyProject" /MIR /XF *.tmp *.bak /XD obj bin

Key Feature 4: Logging

For any automated script, logging is essential. Robocopy has excellent, built-in logging that gives you a detailed summary of the operation.

  • /LOG:<File>: Creates a new log file, overwriting it if it exists.
  • /LOG+:<File>: Appends to an existing log file. This is great for keeping a running history.
  • /NP: No Progress. Prevents the 0%...10%... progress counter from cluttering the log file.
  • /NJH: No Job Header.
  • /NJS: No Job Summary.
ROBOCOPY "C:\Logs" "\\Server\LogArchive" *.log /LOG+:"C:\Scripts\archive.log" /NP

A Robust Robocopy Command Template

This is a professional-grade, "go-to" Robocopy command that combines all the best practices for a reliable backup or mirroring task.

@ECHO OFF
SET "Source=C:\Users\Admin\Documents"
SET "Destination=E:\Backups\Documents"
SET "LogFile=%~dp0Robocopy_Log.txt"

REM A robust command with all the key features:
REM /MIR: Mirror the directories.
REM /EFSRAW: Copy encrypted files correctly.
REM /R:2 /W:5: Set a low retry count for locked files.
REM /NP: Keep the log clean.
REM /LOG+: Append to a running log.
REM /XF: Exclude junk files.
ROBOCOPY "%Source%" "%Destination%" /MIR /EFSRAW /R:2 /W:5 /NP /LOG+:"%LogFile%" /XF "thumbs.db" "~$*"

ECHO.
ECHO Robocopy operation complete.
ECHO Check "%LogFile%" for details.
ECHO Exit Code: %ERRORLEVEL%

Interpreting the Exit Code: Remember that Robocopy uses a bitmask for its exit code. Any value less than 8 indicates that the job completed without any fatal errors.

Conclusion

Robocopy is an essential tool for any serious batch scripter. It is the powerful, reliable, and flexible solution for all file and directory copying tasks.

Key features to remember:

  • Use /MIR for synchronization, but be aware that it deletes files in the destination.
  • Use /R and /W to build resilience against locked files.
  • Use /XF and /XD to exclude unwanted files and folders from your copy.
  • Use /LOG+ to create a detailed, historical log of all copy operations.

By replacing XCOPY with Robocopy in your scripts, you will dramatically increase their reliability and professionalism.