robocopy - Advanced Copy

Maintained on

When you want to back up or mirror folders, copy and xcopy can fall short.

ROBOCOPY (Robust File Copy) is a powerful file copy command built into Windows. It offers mirroring, incremental copying, retry control and logging for dependable copies.

This article covers robocopy from the basics to practical backup use, with sample code.

If you just want to copy a folder with everything in it, robocopy source destination /E is the basic form. To make the destination an exact copy, use /MIR. To judge the result, see the exit codes section.

robocopy syntax

ROBOCOPY source destination [file] [options]
ElementDescription
sourceSource folder path
destinationDestination folder path
fileFile name to copy (wildcards allowed; default is *.*)
optionsOptions that control the behavior

Basic example

robocopy C:\Source D:\Backup
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>robocopy C:\Source D:\Backup
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : Wednesday, March 11, 2026 10:00:00 AM
Source : C:\Source\
Dest : D:\Backup\
Files : .
Total Copied Skipped Mismatch FAILED Extras
Dirs : 3 1 2 0 0 0
Files : 5 5 0 0 0 0
C:\users\user>

Main options

Copy options

OptionDescription
/ECopy subfolders, including empty ones
/SCopy subfolders (excluding empty ones)
/MIRMirror (delete files that do not exist in the source)
/PURGEDelete destination files that no longer exist in the source
/MOVMove files (delete the originals after copying)
/MOVEMove files and folders
/ZRestartable mode (an interrupted copy can resume)
/MT:nCopy with n threads (1-128; 8 if omitted)
/COPY:flagsChoose what to copy (D: data, A: attributes, T: timestamps, S: security)

Selection options

OptionDescription
/XF fileExclude the specified files
/XD folderExclude the specified folders
/XOExclude older files (incremental copy)
/MAXAGE:nExclude files older than n days
/MINAGE:nExclude files newer than n days
/MAX:nExclude files larger than n bytes
/LList what would happen without doing it

Retry options

OptionDescription
/R:nRetries on failure (default: 1,000,000)
/W:nWait between retries in seconds (default: 30)

Log options

OptionDescription
/LOG:fileWrite the log to a file (overwrite)
/LOG+:fileAppend the log to a file
/TEEOutput to both the console and log file
/NPDo not show the progress percentage
/NFLDo not log file names
/NDLDo not log directory names
/VVerbose output
Watch the default value of /R

The default for /R is 1,000,000 retries. If a network connection drops during a copy, robocopy may keep retrying for a very long time. In real use, always set it explicitly, for example /R:3.

Trailing backslash before a closing quote

When a quoted path ends with \", the \ is treated as an escape for the " and the argument is not passed correctly. Do not write "C:\My Folder\"; write "C:\My Folder" without the trailing \.

Copy including subfolders (/E)

The /E option copies subfolders recursively, including empty ones.

robocopy C:\Source D:\Backup /E

Mirroring (/MIR)

The /MIR option reproduces the source folder structure exactly at the destination (it combines /E and /PURGE). Files that exist only in the destination are deleted.

robocopy C:\Source D:\Backup /MIR
Cautions for /MIR

/MIR deletes files and folders that exist only in the destination. If you get the destination path wrong, unintended files may be deleted. Always do a test run with /L the first time.

Test run (/L)

With /L, nothing is copied; robocopy only shows which files would be copied or deleted.

robocopy C:\Source D:\Backup /MIR /L
チェック

Always do a test run with /L before running a real backup, especially with /MIR or /PURGE, to avoid unexpected deletions.

Speed up copying (/MT)

/MT:n copies several files in parallel. It is especially effective when there are many small files.

robocopy C:\Source D:\Backup /E /MT:16
Logs with /MT

With /MT, several threads write to the log at the same time, so the order of log lines may be mixed.

Logging

How to record robocopy results in a log file.

robocopy C:\Source D:\Backup /E /LOG:"C:\Logs\backup.log" /TEE /NP
OptionDescription
/LOG:Specify the log file (overwrite)
/TEEShow output on the console and in the log
/NPHide progress percentages (a cleaner log)

To append to the log, use /LOG+:.

robocopy C:\Source D:\Backup /E /LOG+:"C:\Logs\backup.log" /TEE /NP

Exclude files and folders

Exclude files (/XF)

robocopy C:\Source D:\Backup /E /XF *.tmp *.log

Exclude folders (/XD)

robocopy C:\Source D:\Backup /E /XD "C:\Source\temp" "C:\Source\.git"

Practical example: automatic backup batch

A practical backup batch file using robocopy.

@echo off
setlocal

rem Backup settings
rem Note: the format of %DATE% depends on regional settings. This example assumes yyyy/mm/dd.
set SOURCE=C:\Users\user\Documents
set DEST=D:\Backup\Documents
set LOGFILE=D:\Backup\Logs\backup_%DATE:~0,4%%DATE:~5,2%%DATE:~8,2%.log

rem Create the log folder if it does not exist
if not exist "D:\Backup\Logs" mkdir "D:\Backup\Logs"

echo Starting backup...
echo Source: %SOURCE%
echo Destination: %DEST%

robocopy "%SOURCE%" "%DEST%" /MIR /R:3 /W:5 /LOG+:"%LOGFILE%" /TEE /NP /XD ".git" "node_modules"

rem Check the exit code
IF %ERRORLEVEL% LSS 8 (
    echo Backup completed successfully.
) ELSE (
    echo An error occurred. Please check the log.
)

endlocal

robocopy exit codes

robocopy returns its own exit codes. Unlike most commands, a non-zero code can still mean success. The code is a combination of these bits:

ValueMeaning
1One or more files were copied successfully
2Extra files or folders were found in the destination
4Mismatched files or folders were detected
8Some files could not be copied (copy errors)
16Fatal error (robocopy could not proceed, e.g. invalid path, no access, bad syntax)

For example, exit code 3 means 1 (files copied) + 2 (extra files found). 0 means nothing needed to be copied (everything is identical).

Judging the exit code

Exit codes 0-7 are not failures; 8 or higher means an error. In a batch file, write IF %ERRORLEVEL% LSS 8. When /MIR deletes extra files, the code includes 2, which is not an error either.

robocopy vs. xcopy

Featurerobocopyxcopy
MirroringYes (/MIR)No
Retry controlYes (/R, /W)No
LoggingYes (/LOG, /TEE)No
Restartable modeYes (/Z)Yes (/Z)
MultithreadingYes (/MT)No
Incremental copyYes (/XO)Yes (/D)
Network resilienceExcellentBasic
Exit codesDetailed (0-16)Basic (0-5)

Microsoft has deprecated xcopy and recommends robocopy, so prefer robocopy for new batch files.

Summary

This article explained how to use the robocopy command.

robocopy is a powerful copy tool built into Windows. For backups in particular, combining mirroring, logging and retry control gives you a dependable setup.

Practice question

Practice Problem

Which statement about judging robocopy exit codes is generally correct?

回答がサーバーに送信されることはありません
#Command Prompt #Batch Files #Command Line #Commands