xcopy - Advanced File Copy
What Is the xcopy Command
The xcopy command is an advanced command for copying files and directories. To copy a folder together with its contents, the basic form is xcopy source destination /s /e /i. Compared with the basic copy command, it has the following features:
- Copying including subdirectories: copies a whole directory structure recursively
- Rich options: filtering by date or archive attribute, error handling and more
- Backup support: supports incremental backups that copy only changed files
- Network support: can resume an interrupted copy over a network (
/z)
This article explains xcopy from basic usage to practical combinations of options.
xcopy Basics
The basic syntax of xcopy is as follows.
xcopy [source] [destination] [options]
Copying a Single File
The simplest usage is copying a single file.
This copies document.txt into the C:\Backup\ directory.
Copying a Whole Directory
The /s option copies a directory including its subdirectories.

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超 入門

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門
This copies MyFolder and all of its non-empty subdirectories.
The basic copy command cannot handle subdirectories, while xcopy can copy a whole directory structure with /s. See
for details.
xcopy is still useful, but robocopy is better suited when you need retries on failure or mirroring.
Main Options
/s - Copy Subdirectories
Copies non-empty subdirectories as well.
/e - Copy Empty Subdirectories Too
Used together with /s to copy empty subdirectories as well.
Combining /s /e copies the complete directory structure, including empty directories. This is useful for backups and for duplicating template directories.
/i - Treat the Destination as a Directory
If the destination does not exist, it is created as a directory.
With this option, the “Does destination specify a file name or directory name?” prompt no longer appears.
If the destination does not exist and has no trailing backslash, xcopy may ask whether to treat it as a file or a directory. In batch files, add /i to remove the ambiguity.
/y - Skip the Overwrite Prompt
Overwrites existing files automatically, without a confirmation prompt.
With /y, existing files are overwritten without confirmation. Take care not to overwrite important files by mistake.
/d - Incremental Copy by Date
Copies a file only if the source is newer than the destination file (or the file does not exist at the destination).
You can also copy only files changed on or after a given date.
The /d:m-d-y format is always month-day-year, regardless of the Windows regional settings. In the example above, 04-21-2026 is April 21, 2026. With /d alone and no date, only files newer than the destination are copied.
/m - Copy Only Files with the Archive Attribute
/m copies only files that have the archive attribute set, and then clears that attribute.
/m is sometimes used for backups. To understand the archive attribute, see
as well.
/u - Update Only Existing Files
Updates only files that already exist in the destination.
/h - Include Hidden and System Files
Also copies hidden and system files, which are normally skipped.
/k - Keep the Read-Only Attribute
Applies the source file’s read-only attribute to the destination file.
/r - Overwrite Read-Only Files
Also overwrites files that have the read-only attribute.
/v - Verify the Copy
Verifies that each file was copied correctly.
/q - Quiet Mode
Does not display file names while copying.
/f - Display Full Paths While Copying
Displays the full paths of the source and destination.
/l - Simulate the Copy
Displays the list of files that would be copied, without copying anything.
/l lets you see which files would be copied before actually copying. It is useful before copying a large number of files.
/t - Copy Only the Directory Structure
To create only the folder structure without copying files, use /t.
/j - Copy Large Files Without Buffering
/j can help when handling large files.
/exclude - Exclude Paths Containing Specific Strings
You can exclude paths from the copy by using a text file that lists the exclusion conditions.
xcopy Source Destination /s /exclude:exclude.txt
Write one string per line in exclude.txt. A file is excluded when its full source path contains the string, so write entries such as .tmp or \node_modules\.
/c - Continue on Errors
Even if some files fail to copy, for example because access is denied, the remaining files continue to be copied.
/z - Restartable Mode
If the connection drops while copying over a network, the copy can resume after reconnecting.
Practical Examples
Creating a Backup
An example that backs up a whole project folder.
/s /e: copy everything, including empty directories/h: copy hidden files/i: create the destination as a directory automatically/y: overwrite without confirmation
Incremental Backup
An example that backs up only changed files.
With /d, only files newer than those in the destination (including files not yet copied) are copied.
Copying Files Changed Since a Specific Date
This copies only files modified on or after January 1, 2025.
Copying Only the Directory Structure
With /t, no files are copied and only the directory structure is created.
A Complete Copy Including Read-Only Files
/s /e: copy all subdirectories/r: overwrite read-only files/k: keep the read-only attribute/h: copy hidden files
Batch File Examples
Daily Backup Script
@echo off
setlocal
REM Backup settings
set "SOURCE=C:\Projects"
set "DESTINATION=D:\Backup\Projects"
set "LOGFILE=C:\Backup\backup_log.txt"
echo ======================================== >> "%LOGFILE%"
echo Backup started: %DATE% %TIME% >> "%LOGFILE%"
echo ======================================== >> "%LOGFILE%"
REM Run the incremental backup
xcopy "%SOURCE%" "%DESTINATION%\" /s /e /i /d /y /h /r >> "%LOGFILE%" 2>&1
REM Exit code 1 (nothing to copy) only means there were no updates, so it is not treated as an error
if errorlevel 2 (
echo Error: the backup failed >> "%LOGFILE%"
) else (
echo The backup completed successfully >> "%LOGFILE%"
)
echo Backup finished: %DATE% %TIME% >> "%LOGFILE%"
echo. >> "%LOGFILE%"
endlocal
Confirmation Script Before Copying
@echo off
setlocal
set "SOURCE=C:\Source"
set "DESTINATION=C:\Destination"
echo Files to be copied:
echo ================================
xcopy "%SOURCE%" "%DESTINATION%\" /s /e /i /l
echo.
echo Copy the files listed above? (Y/N)
choice /c YN /n
if errorlevel 2 goto :cancel
if errorlevel 1 goto :copy
:copy
echo.
echo Starting the copy...
xcopy "%SOURCE%" "%DESTINATION%\" /s /e /i /y
echo The copy is complete.
goto :end
:cancel
echo The copy was canceled.
goto :end
:end
endlocal
Exit Codes and Troubleshooting
xcopy returns the following exit codes (ErrorLevel) depending on the result. if errorlevel N tests for “N or greater”, so check from the largest value down.
| Exit code | Description |
|---|---|
| 0 | Files were copied without error |
| 1 | No files were found to copy |
| 2 | The user pressed Ctrl+C to terminate xcopy |
| 4 | An initialization error occurred (insufficient memory or disk space, invalid syntax, etc.) |
| 5 | A disk write error occurred |
Error Handling Example
@echo off
xcopy Source Destination /s /e /i /y
if errorlevel 5 goto :disk_error
if errorlevel 4 goto :init_error
if errorlevel 2 goto :user_cancel
if errorlevel 1 goto :no_files
goto :success
:disk_error
echo Error: a disk write error occurred
goto :end
:init_error
echo Error: initialization error (insufficient memory or disk space, invalid syntax, etc.)
goto :end
:user_cancel
echo Canceled by the user
goto :end
:no_files
echo No files were found to copy
goto :end
:success
echo The files were copied successfully
:end
Summary
This article covered the following about the xcopy command:
- Basic usage: copying files and directories
- Main options: subdirectory copy, date filtering, attribute handling, structure-only copy and more
- Practical examples: backups, incremental copy, duplicating a directory structure
- Batch file use: daily backup scripts and more
- Error handling: handling exit codes properly
xcopy offers more advanced copying than copy, and is very useful for automating backups and file management. Combining options such as /s /e /d lets you build an efficient incremental backup.
If you need more advanced copying or mirroring, consider robocopy, which offers even more features than xcopy.