TExcelTAD
Home › Windows CMD & Batch › xcopy & robocopy

xcopy & robocopy

copy handles single files and wildcards in one folder, but it can't copy a folder tree. For that, Windows has two external tools: xcopy, the older "extended copy", and robocopy ("robust file copy"), the modern tool for backups, mirroring and large or unreliable copies.

xcopy: copy folders with their subfolders

xcopy source destination copies files; the switches decide how much of the tree comes along. The most important ones:

Switch Meaning
/s include subfolders (except empty ones)
/e include subfolders, including empty ones (use with /s or alone)
/i if the destination doesn't exist, assume it is a folder (no question)
/y overwrite existing files without asking
/q don't list the files while copying
/t copy only the folder structure, no files
/exclude:list.txt skip files whose path contains any string listed in list.txt
/d copy only files newer than the destination copy
/h /k also copy hidden/system files, keep the read-only attribute

On Windows, xcopy lists each file and ends with a summary such as 4 File(s) copied. As with copy, the runnable examples send that report to nul and inspect the result instead:

cmd · try it yourself
@echo off
mkdir demo & cd demo
mkdir site\css site\img site\drafts
echo ^<h1^>Home^</h1^>> site\index.html
echo body{}> site\css\main.css
echo png> site\img\logo.png
xcopy site backup /s /i >nul
echo --- with /s (empty drafts folder skipped):
dir /b /o:n /s backup
xcopy site full /e /i >nul
echo --- with /e:
dir /b /o:n full
--- with /s (empty drafts folder skipped):
C:\Users\learner\demo\backup\css
C:\Users\learner\demo\backup\img
C:\Users\learner\demo\backup\index.html
C:\Users\learner\demo\backup\css\main.css
C:\Users\learner\demo\backup\img\logo.png
--- with /e:
css
drafts
img
index.html

Without /i, xcopy doesn't know whether backup should be a file or a folder and asks:

cmd
C:\Users\ada\demo>xcopy site backup /s
Does backup specify a file name
or directory name on the target
(F = file, D = directory)? d
site\index.html
site\css\main.css
site\img\logo.png
3 File(s) copied

A trailing backslash on the destination (backup\) also tells xcopy it is a folder.

Skipping files with /exclude

/exclude: takes a text file with one search string per line. Any file whose full path contains one of the strings is skipped. That makes it easy to leave out temporary files or whole folders:

cmd · try it yourself
@echo off
mkdir demo & cd demo
mkdir app\node_modules\lib app\src
echo code> app\src\main.js
echo dep> app\node_modules\lib\index.js
echo tmp> app\debug.tmp
(
  echo \node_modules\
  echo .tmp
) > skip.txt
xcopy app release /s /i /exclude:skip.txt >nul
echo Files in the release folder:
dir /b /o:n /s /a:-d release
Files in the release folder:
C:\Users\learner\demo\release\src\main.js

The exclude file name itself can't contain spaces; put it in a folder without spaces or next to the script.

Copying only the folder structure

/t /e recreates the empty folder skeleton, which is useful to prepare a destination layout:

cmd · try it yourself
@echo off
mkdir demo & cd demo
mkdir year\q1 year\q2 year\q3
echo report> year\q1\report.xlsx
xcopy year template /t /e /i >nul
dir /b /o:n /s template
C:\Users\learner\demo\template\q1
C:\Users\learner\demo\template\q2
C:\Users\learner\demo\template\q3

Microsoft calls xcopy deprecated in favour of robocopy, but it is on every Windows system and still common in build scripts.

robocopy: robust copies and mirrors

robocopy has a different argument order: source folder, destination folder, then optional file patterns, then switches. It copies only files that are new or changed, retries on network errors, keeps timestamps and attributes, and can mirror a folder exactly:

cmd
robocopy <source> <destination> [files...] [options]
Switch Meaning
/s / /e include subfolders / including empty ones
/mir mirror: like /e, plus delete files in the destination that no longer exist in the source
/purge delete destination files that are not in the source
/xd dir… / /xf file… exclude folders / files (wildcards allowed)
/z restartable mode for large files over the network
/mt:16 copy with 16 threads
/r:2 /w:5 retry 2 times, wait 5 s (the defaults are 1,000,000 retries and 30 s!)
/l list only: show what would happen, change nothing
/np /nfl /ndl no percentage, no file list, no folder list (quieter logs)
/log:file.txt write the report to a file

A typical backup command and its report on Windows 11 (sample output; your paths, sizes and times will differ):

cmd
C:\Users\ada>robocopy C:\Users\ada\Projects\site D:\Backup\site /mir /r:2 /w:5 /xd node_modules

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows
-------------------------------------------------------------------------------

  Started : Saturday, October 17, 2026 9:41:05 AM
   Source : C:\Users\ada\Projects\site\
     Dest : D:\Backup\site\

    Files : *.*

 Exc Dirs : node_modules

  Options : *.* /S /E /DCOPY:DA /COPY:DAT /PURGE /MIR /R:2 /W:5

------------------------------------------------------------------------------

	  New Dir          2	C:\Users\ada\Projects\site\
	    New File  		     214	index.html
	    New File  		    1532	main.js
	  New Dir          1	C:\Users\ada\Projects\site\css\
	    New File  		    4210	main.css

------------------------------------------------------------------------------

               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :         2         2         0         0         0         0
   Files :         3         3         0         0         0         0
   Bytes :     5.8 k     5.8 k         0         0         0         0
   Times :   0:00:00   0:00:00                       0:00:00   0:00:00
   Ended : Saturday, October 17, 2026 9:41:05 AM

Run the same command again and every file shows up under Skipped, because nothing changed. That is what makes robocopy ideal for repeated backups.

Warning: /mir deletes files in the destination. Mirroring an empty or wrong source folder onto a backup wipes the backup. Run with /l first to preview, and double-check the order: source first, destination second.

robocopy exit codes

Unlike most commands, robocopy uses its exit code as a bit field, and non-zero does not mean failure:

Code Meaning
0 nothing to copy, source and destination already in sync
1 files were copied successfully
2 extra files or folders found in the destination
4 mismatched files or folders found
8 some files or folders could not be copied (errors)
16 fatal error: nothing was copied (bad path, no access)

Codes combine: 3 means 1 + 2. The rule for scripts is 8 or higher means trouble:

cmd
robocopy "%SRC%" "%DST%" /mir /r:2 /w:5 /np /log:backup.log
if %errorlevel% geq 8 (
  echo Backup FAILED with code %errorlevel%, see backup.log
  exit /b 1
)
echo Backup OK
exit /b 0

The exit /b 0 matters in scheduled tasks and CI pipelines, which would otherwise treat robocopy's "success" codes 1–7 as failures.

Which one should I use?

Need Use
one file or a few files in one folder copy
a folder tree in a simple build script xcopy /s /e /i /y
backups, mirrors, large or network copies, logs robocopy
copying with objects and filters in a script PowerShell Copy-Item -Recurse

Learn more: Copy, Move & Rename · Exit Codes & errorlevel · Bash cp & mv

Test yourself

Which xcopy switch includes empty subfolders?

  1. /s
  2. /e
  3. /i
  4. /t

What does robocopy src dst /mir do that /e doesn't?

  1. Copies hidden files
  2. Deletes files in dst that are no longer in src
  3. Copies only newer files
  4. Copies in reverse direction

A script runs robocopy and gets exit code 1. What happened?

  1. A fatal error
  2. Files were copied successfully
  3. Nothing was copied because of a bad path
  4. Some files failed