XCOPY.EXE: complete guide to Windows file copying command

XCOPY.EXE is the built-in Windows command that copies files and directories, including subdirectories; run xcopy C:\Source D:\Backup /s /e /i to copy a whole folder tree.

This guide covers its syntax, the switches that matter, worked examples, batch scripting with exit codes, common errors, and when robocopy is the better tool.

Copy a folder and all its subfolders with XCOPY

This is the command most people need. It copies every file and subfolder, including empty ones, and creates the destination folder if it does not exist.

  1. Select Start, type cmd and open Command Prompt.
  2. Type xcopy "C:\Source" "D:\Backup\Source" /s /e /i /h and press Enter. Replace both paths with your own.
  3. Wait for the final line, which reports how many files were copied, for example 3 File(s) copied.
  4. Add /y if you are re-running the copy and do not want an overwrite prompt for every existing file.

/s copies subdirectories, /e keeps empty ones, /i treats the destination as a folder, and /h includes hidden and system files.

What XCOPY.EXE Is and When to Use It

Fact Details
What it does Copies files and directories, including subdirectories
Supported Windows versions Windows 11, Windows 10, and Windows Server 2016 through 2025
Installation None; it ships with Windows and runs from Command Prompt or a batch file
Good for Copying folder trees, updating only changed files, previewing a copy with /l, copying to disks of a different format
Less suited to Mirroring folders, very large or unreliable network jobs, and copies that need logs or retries
Versus diskcopy diskcopy needs identical disk formats; xcopy does not

If you are new to the command line, the guide to cmd.exe, the Windows command processor explains how Command Prompt runs commands like this one.

XCOPY Syntax and Command Structure

The source is required and must include a drive or path. The destination is optional; if you leave it out, xcopy copies into the current directory.

xcopy <Source> [<Destination>] [/w] [/p] [/c] [/v] [/q] [/f] [/l] [/g] [/d[:MM-DD-YYYY]] [/u] [/i] [/s [/e]] [/t] [/k] [/r] [/h] [{/a | /m}] [/n] [/o] [/x] [/exclude:File1[+File2]] [{/y | /-y}] [/z] [/b] [/j] [/compress]

Source first, then the destination, then any switches in any order. Wrap paths that contain spaces in double quotes. Run xcopy /? to print the built-in help.

You should see: The command ends with a line such as 3 File(s) copied. If it asks whether the destination is a file or a directory, press D for a folder, or add /i next time.

Microsoft Learn xcopy reference page showing the command syntax
The xcopy page on Microsoft Learn lists the full syntax and parameter table, and applies to Windows 10 and Windows 11. (Image: Microsoft)

Essential XCOPY Switches and Options

Switch What it does
/s Copies directories and subdirectories, unless they are empty
/e Copies all subdirectories, even empty ones; use it with /s or /t
/i Assumes the destination is a directory and creates it when the source is a folder or uses wildcards
/h Includes hidden and system files, which are skipped by default
/y / /-y Overwrites without asking / always asks before overwriting
/d[:MM-DD-YYYY] Copies files changed on or after the date; with no date, only files newer than the destination copy
/u Copies only files that already exist in the destination
/l Lists what would be copied without copying anything
/c Keeps going when an error occurs
/v Verifies each file as it is written
/k Keeps the read-only attribute; by default xcopy removes it
/r Overwrites read-only files
/t Copies the folder structure only, no files; add /e for empty folders
/o / /x Copies ownership and DACL permissions / also audit settings (SACL)
/exclude:list.txt Skips any file whose path contains a string listed in the text file
/z Copies over a network in restartable mode and shows per-file progress
/j Copies without buffering, recommended for very large files
/a / /m Copies only files with the archive attribute; /m also clears it on the source
/q / /f Hides xcopy messages / shows full source and destination names

Common XCOPY Examples for Files and Folders

Task Command
Copy a whole drive or folder tree with empty folders xcopy a: b: /s /e
Same, including hidden and system files xcopy a: b: /s /e /h
Update only files changed since a date xcopy \rawdata \reports /d:12-29-1993
Refresh files that already exist in the destination xcopy \rawdata \reports /u
Preview a copy and save the list to a file xcopy \rawdata \reports /d:12-29-1993 /l > xcopy.out
Copy to a network drive, keep read-only, create the folder xcopy \customer h:\public\address /s /e /k /p /i
Copy files matching a pattern from every subfolder xcopy .\toc*.yml ..\..\Copy-To\ /S /Y
Recreate the folder structure without files xcopy C:\Source D:\Empty /t /e

The wildcard example needs the asterisk; .\toc.yml on its own copies nothing from subfolders.

Using XCOPY in Batch Files and Automation

xcopy returns an exit code that a batch file reads with if errorlevel. Check the highest codes first, because if errorlevel 2 is true for 2 and anything above it.

@echo off
xcopy %1 %2 /s /e
if errorlevel 4 goto lowmemory
if errorlevel 2 goto abort
if errorlevel 0 goto exit
:lowmemory
echo Insufficient memory to copy files or
echo invalid drive or command-line syntax.
goto exit
:abort
echo You pressed CTRL+C to end the copy operation.
goto exit
:exit

Save it as copyit.bat, then run copyit c:\prgmcode b:. Windows substitutes the first path for %1 and the second for %2, and the script reports an error message based on the exit code.

You should see: With no error, the script exits silently after xcopy prints its file count.

Add /y and /i to unattended jobs so xcopy never stops to ask a question. You can also set /y in the COPYCMD environment variable; /-y on the command line overrides it.

What each XCOPY exit code means

Exit code Meaning
0 Files were copied without error
1 No files were found to copy
2 The user pressed Ctrl+C to stop xcopy
4 Initialization error: not enough memory or disk space, an invalid drive name, or invalid syntax
5 Disk write error

How to check the copy worked

  1. Read the last line xcopy prints and compare the file count with what you expected.
  2. Run echo %errorlevel% straight after xcopy; 0 means no errors.
  3. Run the same command again with /l added; an empty list means nothing is left to copy with /d.
  4. Use /v on important copies so each file is verified as it is written.

Handling Errors, Prompts, and Permissions

"Does <Destination> specify a file name or directory name on the target?"

The destination does not exist and does not end with a backslash.

  1. Press D to copy into a folder, or F to copy into a single file.
  2. Add /i to the command, or end the destination path with \, so the prompt never appears.

An overwrite prompt appears for every file

xcopy asks before overwriting by default.

  1. Add /y to overwrite without prompting.
  2. Add /d instead if you only want newer files to replace older ones.

Access denied or files are skipped

Your account lacks permission, or the files are hidden, system or read-only.

  1. Open Command Prompt with Run as administrator and run the command again.
  2. Add /h for hidden and system files and /r to overwrite read-only files.
  3. Add /c to continue past the files that still fail.

"Insufficient memory" error

A file or folder path is longer than 255 characters.

  1. Shorten the folder names or copy from a deeper starting folder.
  2. Use robocopy for trees with very long paths.

Error copying encrypted files

The destination volume does not support EFS encryption.

  1. Add /g to create decrypted copies on the destination.
  2. Or copy to an NTFS volume that supports EFS.

XCOPY vs Copy vs Robocopy

Your job Use Why
Copy or combine a few files in one folder copy Simplest; it can join several files into one but does not copy subdirectories
Copy a folder tree, empty files or empty folders xcopy Microsoft points to xcopy for 0-byte files and whole directory trees
Mirror a folder so the destination matches exactly robocopy /mir Mirrors a tree and deletes files that no longer exist in the source
Large or long-running network copies robocopy /z /mt Restartable mode plus multi-threading with 1 to 128 threads
Copies that need a log and retries robocopy /log: /r: /w: Writes a log file and retries failures on a schedule
Files blocked by permissions robocopy /b Backup mode overrides file and folder ACLs that block access

Robocopy exit codes work differently: 0 to 7 mean success with different outcomes, and 8 or higher means at least one file failed.

Frequently Asked Questions

Does xcopy work on Windows 11?

Yes. Microsoft lists xcopy as supported on Windows 11, Windows 10 and Windows Server 2016 through 2025. Open Command Prompt and type xcopy followed by a source and destination to use it.

How do I install the xcopy command?

You do not need to. xcopy is built into Windows and runs from Command Prompt or any batch file. Type xcopy /? in Command Prompt to confirm it is there and see its help.

How do I use xcopy?

Type xcopy, the source path, the destination path and any switches, then press Enter. For example, xcopy C:\Source D:\Backup /s /e /i copies a folder with all its subfolders and creates the destination folder.

How do I copy hidden files with xcopy?

Add the /h switch. By default xcopy skips files that have hidden or system attributes, so a full backup command looks like xcopy C:\Source D:\Backup /s /e /h /i.

How do I stop xcopy asking file or directory?

Add /i to the command, or end the destination path with a backslash. With /i, xcopy assumes the destination is a directory when the source is a folder or uses wildcards, and creates it.

Can xcopy copy only new or changed files?

Yes. Use /d with no date to copy only files newer than the copies already in the destination, or /d:MM-DD-YYYY to copy files changed on or after that date.

Is robocopy better than xcopy?

For large, network or mirrored copies, yes. Robocopy adds mirroring, multi-threading, retries, logging and backup mode. xcopy remains fine for quick folder-tree copies and older batch scripts.

Bottom Line

Use xcopy source destination /s /e /i /h for everyday folder-tree copies, and switch to robocopy when you need mirroring, logs, retries or speed on big network jobs. xcopy is built into Windows 11 and 10 and needs no setup, but robocopy has the recovery and logging features long copies depend on.

Philip Celasco

Philip is a Texas-based technology writer and IT administrator at Techdows.com with more than 10 years of experience creating practical content for everyday users and professionals. He specializes in web browsers, particularly Chromium-based platforms such as Google Chrome, Microsoft Edge, Brave, and Opera. Through his work as an IT administrator, Philip has hands-on experience managing devices, configuring browser policies, troubleshooting software and network issues, and helping people resolve problems that affect productivity and security. His articles are based on practical testing and real-world technical experience. He covers browser settings, extensions, performance problems, privacy controls, security features, and Windows troubleshooting. Outside work, Philip enjoys the quieter side of life in Texas and stepping away from the screen when he can. He has two kids, two cats and loves to play golf with his mother during the weekends.

Leave a Reply

Your email address will not be published. Required fields are marked *