xcopy - Advanced File Copy

Maintained on

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.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy document.txt C:\Backup\
document.txt
1 File(s) copied
C:\users\user>

This copies document.txt into the C:\Backup\ directory.

Copying a Whole Directory

The /s option copies a directory including its subdirectories.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy MyFolder C:\Backup\MyFolder\ /s
MyFolder\file1.txt
MyFolder\file2.txt
MyFolder\SubFolder\file3.txt
3 File(s) copied
C:\users\user>
おすすめ書籍※ 広告を含む場合があります
中小企業経営者のためのRPA入門 RPA導入を成功させる方法

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

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

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

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

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

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

This copies MyFolder and all of its non-empty subdirectories.

Difference from the copy command

The basic copy command cannot handle subdirectories, while xcopy can copy a whole directory structure with /s. See

for details.

Use robocopy when you need retries or mirroring

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.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy Source Destination /s

/e - Copy Empty Subdirectories Too

Used together with /s to copy empty subdirectories as well.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy Source Destination /s /e
Replicating a complete directory structure

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.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy Source NewDestination /s /i
Source\file1.txt
Source\file2.txt
2 File(s) copied
C:\users\user>

With this option, the “Does destination specify a file name or directory name?” prompt no longer appears.

Behavior when the destination is ambiguous

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.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy Source Destination /y
Be careful with overwriting

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).

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy Source Destination /d /s

You can also copy only files changed on or after a given date.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy Source Destination /d:04-21-2026 /s
The /d:date format is month-day-year

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.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy Source Destination /s /m
Sometimes used for incremental backups

/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.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy Source Destination /u /s

/h - Include Hidden and System Files

Also copies hidden and system files, which are normally skipped.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy Source Destination /s /e /h

/k - Keep the Read-Only Attribute

Applies the source file’s read-only attribute to the destination file.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy Source Destination /s /k

/r - Overwrite Read-Only Files

Also overwrites files that have the read-only attribute.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy Source Destination /r

/v - Verify the Copy

Verifies that each file was copied correctly.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy Source Destination /v /s

/q - Quiet Mode

Does not display file names while copying.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy Source Destination /s /q
15 File(s) copied
C:\users\user>

/f - Display Full Paths While Copying

Displays the full paths of the source and destination.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy Source Destination /s /f
C:\Source\file1.txt -> C:\Destination\file1.txt
C:\Source\file2.txt -> C:\Destination\file2.txt
2 File(s) copied
C:\users\user>

/l - Simulate the Copy

Displays the list of files that would be copied, without copying anything.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy Source Destination /s /l
Source\file1.txt
Source\file2.txt
Source\SubFolder\file3.txt
3 File(s)
C:\users\user>
Handy for checking beforehand

/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.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy Source Destination /t /e

/j - Copy Large Files Without Buffering

/j can help when handling large files.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy LargeSource LargeDestination /j

/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.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy Source Destination /s /e /c

/z - Restartable Mode

If the connection drops while copying over a network, the copy can resume after reconnecting.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy \\Server\Share C:\Backup\ /s /z

Practical Examples

Creating a Backup

An example that backs up a whole project folder.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy C:\Projects D:\Backup\Projects\ /s /e /h /i /y
  • /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.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy C:\Projects D:\Backup\Projects\ /s /d /y

With /d, only files newer than those in the destination (including files not yet copied) are copied.

Copying Files Changed Since a Specific Date

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy C:\Logs C:\Archive\Logs\ /s /d:01-01-2025

This copies only files modified on or after January 1, 2025.

Copying Only the Directory Structure

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy Source Destination /t /e

With /t, no files are copied and only the directory structure is created.

A Complete Copy Including Read-Only Files

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>xcopy Source Destination /s /e /r /k /h
  • /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 codeDescription
0Files were copied without error
1No files were found to copy
2The user pressed Ctrl+C to terminate xcopy
4An initialization error occurred (insufficient memory or disk space, invalid syntax, etc.)
5A 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.

About the robocopy command

If you need more advanced copying or mirroring, consider robocopy, which offers even more features than xcopy.

Similar Commands

Practice Questions

Practice Problem

Choose the correct command to copy a whole directory, including empty subdirectories.

回答がサーバーに送信されることはありません
Practice Problem

Which option copies only files changed since the last backup (incremental backup)?

回答がサーバーに送信されることはありません
Practice Problem

Which option only displays the list of files that would be copied, without actually copying them?

回答がサーバーに送信されることはありません
#Command Prompt #Batch File #File Operations #Copy #Command