|

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

I want toType this
Copy a folder with all subfolders, even empty onesxcopy C:\Source D:\Backup /s /e /i
Include hidden and system filesxcopy C:\Source D:\Backup /s /e /h /i
Copy only files changed since a datexcopy C:\Source D:\Backup /d:12-29-2026
Preview what would be copiedxcopy C:\Source D:\Backup /l
Overwrite without a promptxcopy 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

  1. Press the Windows key and type cmd.
  2. Select Command Prompt.
  3. 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

SwitchWhat it does
/sCopies directories and subdirectories, except empty ones.
/eCopies all subdirectories, including empty ones.
/tCopies the directory structure only, not the files; add /e to include empty directories.
/iTreats 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

SwitchWhat it does
/hCopies hidden and system files, which are not copied by default.
/rCopies read-only files.
/kKeeps the read-only attribute on destination files; by default xcopy removes it.
/oCopies file ownership and DACL information.
/xCopies 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

SwitchWhat it does
/ySuppresses the prompt to confirm overwriting an existing destination file.
/-yPrompts before overwriting, and overrides /y set in the COPYCMD variable.
/pPrompts you to confirm whether to create each destination file.
/wDisplays 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

SwitchWhat it does
/lLists the files that would be copied without copying them.
/fDisplays source and destination file names while copying.
/vVerifies each file as it is written.
/qSuppresses xcopy messages.
/cIgnores 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

SwitchWhat it does
/aCopies only source files with the archive attribute set, without changing it.
/mCopies files with the archive attribute set, then turns the attribute off on those source files.
/nUses NTFS short (8.3) names, required when copying from NTFS to FAT.
/bCopies 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

CodeMeaning
0Files were copied without error.
1No files were found to copy.
2You pressed Ctrl+C to end xcopy.
4Initialization error: not enough memory or disk space, an invalid drive name or invalid syntax.
5A 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.

Leave a Reply

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