How To Use The Xcopy Command In Windows (2026)
Xcopy copies files and whole folder trees, including subfolders, from Command Prompt.
The most common form is xcopy <source> <destination> /s /e, which copies everything including empty folders.
This guide follows Microsoft’s xcopy reference, so each switch, prompt and exit code is documented.
Table of Contents
- Xcopy quick answers
- The xcopy syntax
- Run xcopy
- Copy folders and subfolders
- The file or directory prompt
- Copy hidden, system and read-only files
- Copy only new or changed files
- Control overwrite prompts
- Preview, verify and skip errors
- Large files and network copies
- Archive attribute, short names and links
- Append files and more examples
- Xcopy or diskcopy
- Exclude files with /exclude
- Xcopy exit codes and errors
- Check the result in a batch file
- Official source
- More Windows guides
Xcopy quick answers
| I want to | Type this |
|---|---|
| Copy a folder with all subfolders, even empty ones | xcopy C:\Source D:\Backup /s /e /i |
| Include hidden and system files | xcopy C:\Source D:\Backup /s /e /h /i |
| Copy only files changed since a date | xcopy C:\Source D:\Backup /d:12-29-2026 |
| Preview what would be copied | xcopy C:\Source D:\Backup /l |
| Overwrite without a prompt | xcopy C:\Source D:\Backup /s /y |
The xcopy syntax
The source is required and must include a drive or a path.
The destination can include a drive letter and colon, a directory name, a file name, or a combination.
If you leave the destination out, xcopy copies the files to the current directory.
Without /s, xcopy works within a single directory.
Run xcopy
- Press the Windows key and type cmd.
- Select Command Prompt.
- Type your xcopy command and press Enter.
Put quotation marks around paths with spaces.
For protected folders, open Terminal as administrator first.
Copy folders and subfolders
| Switch | What it does |
|---|---|
/s | Copies directories and subdirectories, except empty ones. |
/e | Copies all subdirectories, including empty ones. |
/t | Copies the directory structure only, not the files; add /e to include empty directories. |
/i | Treats the destination as a directory and creates it if needed. |
Microsoft’s basic example is xcopy a: b: /s /e.
The file or directory prompt
If the destination does not exist, does not end with a backslash and is not an existing directory, xcopy asks whether it is a file name or a directory name.
Press F to copy to a file or D to copy to a directory.
Add /i to skip this question and let xcopy assume a directory when the source is a directory, has wildcards or is more than one file.
Ending the destination with a backslash, as in D:\Backup\, also tells xcopy it is a folder.
Copy hidden, system and read-only files
| Switch | What it does |
|---|---|
/h | Copies hidden and system files, which are not copied by default. |
/r | Copies read-only files. |
/k | Keeps the read-only attribute on destination files; by default xcopy removes it. |
/o | Copies file ownership and DACL information. |
/x | Copies audit settings and SACL information, and implies /o. |
Copy only new or changed files
The /d:MM-DD-YYYY switch copies source files changed on or after the date you give.
If you omit the date, /d copies source files that are newer than the existing destination files.
That makes it a quick way to update a backup folder.
The /u switch copies only source files that already exist on the destination.
Microsoft’s example xcopy \rawdata \reports /d:12-29-1993 updates \Reports with files changed since that date.
Control overwrite prompts
| Switch | What it does |
|---|---|
/y | Suppresses the prompt to confirm overwriting an existing destination file. |
/-y | Prompts before overwriting, and overrides /y set in the COPYCMD variable. |
/p | Prompts you to confirm whether to create each destination file. |
/w | Displays Press any key to begin copying file(s) and waits. |
Microsoft says you can set /y in the COPYCMD environment variable, and that by default you are prompted to overwrite.
Preview, verify and skip errors
| Switch | What it does |
|---|---|
/l | Lists the files that would be copied without copying them. |
/f | Displays source and destination file names while copying. |
/v | Verifies each file as it is written. |
/q | Suppresses xcopy messages. |
/c | Ignores errors. |
Microsoft’s preview example redirects the output to a file: xcopy \rawdata \reports /d:12-29-1993 /l > xcopy.out.
Large files and network copies
The /j switch copies without buffering, which Microsoft recommends for very large files.
The /z switch copies over a network in restartable mode and shows the percentage completed for each file.
If the connection drops, a restartable copy resumes once the connection is re-established.
For long jobs with logging and retries, the Robocopy command offers more control.
Archive attribute, short names and links
| Switch | What it does |
|---|---|
/a | Copies only source files with the archive attribute set, without changing it. |
/m | Copies files with the archive attribute set, then turns the attribute off on those source files. |
/n | Uses NTFS short (8.3) names, required when copying from NTFS to FAT. |
/b | Copies a symbolic link instead of the file it points to. |
Microsoft notes that xcopy sets the archive attribute on destination files whether or not the source had it set.
Append files and more examples
To append files, give one destination file and several sources, using wildcards or the file1+file2+file3 form.
Microsoft’s example xcopy \customer h:\public\address /s /e /k /p copies a folder tree, keeps read-only attributes and prompts for each new file.
Adding /i to that command creates the Address folder if needed and suppresses the new-directory prompt.
Another example, xcopy .\toc*.yml ..\..\Copy-To\ /S /Y, gathers every matching file from the subfolders into one target folder.
Microsoft points out that removing the wildcard from that example would copy nothing.
Xcopy or diskcopy
Diskcopy copies track by track, so the source and destination must have the same format.
Xcopy has no such requirement, so it is the right tool for ordinary folders.
Exclude files with /exclude
The /exclude:FileName1[+FileName2] switch points to one or more text files of search strings.
Each string goes on its own line, and a file is skipped if a string matches any part of its absolute path.
Microsoft’s example is that the string obj excludes everything under an obj directory and every .obj file.
At least one exclusion file must be named.
Xcopy exit codes and errors
| Code | Meaning |
|---|---|
| 0 | Files were copied without error. |
| 1 | No files were found to copy. |
| 2 | You pressed Ctrl+C to end xcopy. |
| 4 | Initialization error: not enough memory or disk space, an invalid drive name or invalid syntax. |
| 5 | A disk write error occurred. |
Microsoft adds that you may see an insufficient memory error when the path of a file or folder is longer than 255 characters.
Copying encrypted files to a volume that does not support EFS fails, so decrypt them first or use /g to create decrypted destination files.
Check the result in a batch file
Use ErrorLevel with an if statement to react to the codes above.
Because if errorlevel tests for a value greater than or equal to the number, Microsoft’s sample checks the highest code, 4, first.
Run xcopy %1 %2 /s /e inside the batch file, then test for 4, then 2, then 0.
Official source
All switches above come from Microsoft’s xcopy reference.
Its syntax notation is explained in the Command-Line Syntax Key.
More Windows guides
- How To Uninstall ESET From Windows Step By Step (2026)
- How To Uninstall Kaspersky Completely From Windows (2026)
- Local Security Authority Protection Is Off In Windows 11 (2026)
- How To Use Ping And Tracert In Windows 11 (2026)
- How To Change UAC Settings In Windows 11 (2026)
- How To Rip A CD To Your PC In Windows 11 (2026)
See all: Windows 11 Guides