Xcopy - DOS/Command Prompt Reference
Copies files and directories, supporting the copying of multiple files and the bulk copying of directories.
Xcopy has the capability to copy files similar to Copy command, but it is more feature-rich than the Copy command. It supports copying multiple files, bulk copying of directories, and offers additional functionality beyond the Copy command. For example,
xcopy /S /E C:\Foo N:\Bar\
... by specifying options and providing the ‘source’ and ‘destination’ paths like the above, you can perform the copy operation using Xcopy (the options can be appended at the end such as ‘C:\Foo N:\Bar\ /S /E’).
One of the features of Xcopy is the abundance of options for controlling copy operations compared to the Copy command. The representative option includes as follows:
- /Y : Force overwrite
- /S : Copy subdirectories (empty directories are not copied)
- /E : Copy subdirectories, including empty ones (also achievable with ‘/S /E’)
- /Q : Suppress screen output during copying
For more samples, please see ‘Samples’ section.
xcopy[.exe] <source> [<destination>] [/A | /M] [/D[:<date>]] [/P] [/S] [/E] [/V] [/W] [/Y] [/-Y] [/C] [/I] [/Q] [/F] [/L] [/H] [/R] [/T] [/U] [/K] [/N] [/G] [/O] [/X] [/Z] [/B] [/J] [/EXCLUDE:<file1>[+<file2>]...] [/COMPRESS]
Specifies the source file/directory name for copying. Wildcards can be used to specify multiple files. Furthermore, the specification ‘<drive>:’ (e.g. ‘C:’) indicates copying all files within the drive. Note that device file names (such as CON or NUL) cannot be specified.
If the source is a directory, the following file-related options apply to each file within the directory.
[MS-DOS] <source> cannot end with ‘\’. If you want to copy the contents of a directory, either end the target directory name or use a wildcard such as ‘\*.*’.
[Window 95以降] If you end <source> with ‘\’, it performs the same as ‘*.*’ for the contents of that directory.
|<destination>||Specifies the destination file/directory name for copying. Note that if the end of the name specified in ‘<destination>’ is terminated with ‘\’, files/directories will be copied into that directory, but if it does not end with ‘\’, a prompt ‘Does <destination> specify a file name or directory name on the target (F = file, D = directory)?’ will be displayed (This includes cases where <destination> includes an extension such as ‘file.ext’).|
|/A||Copies only files with the ‘Archive attribute’. Cannot be used in conjunction with /M.|
|/M||Copies only files with the ‘Archive attribute’ and clear the Archive attribute of the source files (leaving the Archive attribute of the destination files unchanged). Cannot be used in conjunction with /A.|
Copy only files with a date (modification date) on or after <date>. If omitting ‘:<date>’, the copy is performed only if the source date is newer than the destination date.
[MS-DOS] <date> cannot be omitted. Also, the format of <date> depends on the COUNTRY setting in the Config.sys file.
[Windows NT series / Windows XP or later] The format of <date> is ‘month-day-year’.
|/P||Displays a confirmation prompt before each file copy.|
|/S||Copies directories and subdirectories contained in the source. If /E is not specified, empty directories will not be copied.|
|/E||Copies directories and subdirectories contained in the source. In this case, even if the directory to be copied is empty, it will be copied. ‘/S /E’ is considered equivalent to ‘/E’.|
|/V||[MS-DOS, Windows NT series / Windows XP or later] Verifies that each file copied from the source is identical to the corresponding file at the destination. ([Windows NT series / Windows XP or later] Verifies only if the file sizes match.)|
|/W||Displays a prompt that requests any key input before starting the copy. This prompt allows cancellation using Ctrl+C.|
|/Y||Forcefully overwrites the destination files without displaying the confirmation message when the destination file already exists.|
|/-Y||Displays a confirmation message to overwrite the destination files if they already exist (even if the ‘/Y’ switch is set in the COPYCMD environment variable).|
|/C||[Windows 95 or later] Continues copying the remaining files even if an error occurs during the copy operation.|
|/I||[Windows 95 or later] If the file/directory specified in <destination> does not exist, treats it as a directory name and copies into that directory.|
|/Q||[Windows 95 or later] Does not display file names during copying (Quiet mode).|
|/F||[Windows 95 or later] Displays all source and destination file names during copying.|
|/L||[Windows 95 or later] Displays all file names that would be copied without actually performing the copy operation.|
|/H||[Windows 95 or later] Copies hidden and system files from the source if they are included.|
|/R||[Windows 95 or later] Overwrites read-only files in the destination if they exist.|
|/T||[Windows 95 or later] Creates only the directory structure without copying files (creates the same directory tree as the source). If /E is not specified, empty directories existing in the source are not created. If /E is specified, empty directories are also created.|
|/U||[Windows 95 or later] Updates destination files. When this option is specified, if a file exists in both the source and destination, the file is not copied if the source file is older.|
|/K||[Windows 95 or later] Copies the source file attributes as well. If /K is not specified, the ‘Read-only’ attribute is removed.|
|/N||[Windows 95 or later] When copying files with file names in formats other than MS-DOS format (such as long file names), tries to use short file names whenever possible.|
|/G||[Windows NT series / XP or later] Copies encrypted files from the source, even if the destination does not support encryption.|
|/O||[Windows NT series / XP or later] Copies file ownership and ACL information (access rights, etc.) as well.|
|/X||[Windows NT series / XP or later] Copies file audit settings as well. If /X is specified, it also implies /O.|
|/Z||[Windows NT series / XP or later] Copies network files in restartable mode. In case of communication failures or other issues leading to copy failure, the copy operation can be resumed using the same file name.|
|/B||[Windows NT series / XP or later] If there are symbolic links in the source, copies the symbolic links themselves rather than their targets.|
|/J||[Windows NT series / XP or later] Performs the copy using unbuffered I/O. Suitable for copying very large files.|
[Windows NT series / XP or later] Specifies a text file containing patterns for files to be excluded during the copy of multiple files. Each pattern should be on a separate line in the file. Multiple patterns can be specified by connecting them with ‘+’.
If the pattern characters in the text file match any part of the absolute path, the corresponding files will be excluded from the copy. For example, the entry ‘\temp\’ means that files within any location's ‘temp’ directory will be excluded from the copy. If ‘.obj’ is specified, files with the extension ‘.obj’ or files with ‘.obj’ in their names will be excluded.
[Windows 10 2004 / 17763.1432? or later] Performs the copy operation using ‘SMB Compression’ (Server Message Block Compression) if available, which compresses data during network copy operations, helping to save bandwidth and improve transfer speeds.
* To use this feature, the destination must support SMB Compression.
|/SPARSE||[Windows 11 22H2? or later] When copying sparse files, preserves the sparse state in the destination. If this option is not specified, the destination file becomes non-sparse when copying a sparse file.|
Xcopy is a program for copying files and directories that allows for more detailed specifications than Copy command. Unlike Copy, Xcopy also allows copying files along with their directory structure.
[MS-DOS] Xcopy copies 0-byte files (Copy command cannot).
[Windows XP? or later] If you need to perform file selection based on file attributes, timestamp, or mirror the source and destination, consider using Robocopy.
Prompt whether the target is file or directory name
If the name specified for the destination does not end with "\" and the destination does not exist, Xcopy prompts with ‘Does <name> specify a file name or directory name on the target (F = file, D = directory)?’ and asks for action. To avoid this, you can take the following measures:
- If the destination name is intended to be a directory, append ‘\’ to the end.
- [Windows 95 or later] Alternatively, you can use the ‘/I’ option to always treat the destination as a directory.
- If the destination is a file rather than a directory, as in the example of copying a single archive file, you can use the pipe ‘|’ to set the options in advance.
- However, when copying individual files, the Copy command is often more suitable in many cases.
Copying files with Archive attribute
The ‘Archive attribute’ is one of the attributes associated with a file, and signifies that the file is a ‘backup candidate’ or similar. Typically, when a file is created or edited and then saved, the Archive attribute is automatically added. The attribute is removed as part of certain backup operations or related activities.
In Xcopy, it is possible to perform copy operations taking into account this ‘Archive attribute’, and this is done through the use of the /A or /M options. The /A and /M options have the effect of copying only files with the ‘Archive attribute’ to the destination, so this allows for operations such as effectively ‘copying updated files only’. Note that both the /A and /M options have the effect of copying only files with the ‘Archive attribute’; however, the /M option also clears the ‘Archive attribute’ on the source files, marking them as ‘backed up’.
[Windows 95 or later] By adding the /U option, you can use Xcopy to perform ‘update copy’ of files. Specifically, the behavior is such that files are copied if there is no identical file in the destination, or if the destination file is older (in terms of modification timestamp) than the source file. It can be used in backup or file transfer operations to prevent accidentally overwriting files in the destination that have been updated for some reason.
* For similar operations using Robocopy, the '/XO' option is used.
Xcopy sets the exit code (Errorlevel) to 0 for successful completion, and 1 or higher otherwise. Therefore, you can perform error checking using If. (specific error codes are unknown)
xcopy /Y C:\Documents\hoge.txt D:\Backup\
Copy the file ‘C:\Documents\hoge.txt’ into the directory ‘D:\Backup’. The ‘/Y’ option forces unconditional (forceful) overwrite if the file already exists.
xcopy /Y /Q /S /E E:\My\Directory\*.* F:\Foo\Bar\Directory\
Copy the entire contents (files and subdirectories) of the directory ‘E:\My\Directory’ to the directory ‘F:\Foo\Bar\Directory’. With the ‘/S /E’ options, the directory structure is also copied. (The ‘/Y’ option allows unconditional overwrite if some files already exist inside.) Additionally, the ‘/Q’ option suppresses the display of file names during the copy process.
* If ‘F:\Foo\Bar\Directory’ does not exist, it will be automatically created.
xcopy /A /S /E /Y C:\MyDoc\*.* D:\Backup\MyDoc\
Copy all files within ‘C:\MyDoc’ that have the ‘Archive attribute’ to the ‘D:\Backup\MyDoc’ directory. During this process, also copy files located within subdirectories of ‘C:\MyDoc’ (maintaining the same directory structure).
* Please see also samples of Attrib command.
xcopy /Y /Q /S /E /M E:\My\Directory\*.* F:\Foo\Bar\
Copy all files and subdirectories within the directory ‘E:\My\Directory’ that have the ‘Archive attribute’ to the directory ‘F:\Foo\Bar’ (forcefully overwriting if some files already exist inside). The ‘/M’ option is present, so the ‘Archive attribute’ on the source files is removed during the copy process.
echo F| xcopy /M file1.txt file2.txt
If ‘file1.txt’ has the ‘Archive attribute’, copies ‘file1.txt’ and creates ‘file2.txt’. , the prompt ‘Does file2.txt specify a file name or directory name on the target (F = file, D = directory)?’ is displayed, but to automatically input ‘F’, Echo command is used.
Note that after executing the command, the ‘Archive attribute’ of ‘file1.txt’ is removed (while the ‘Archive attribute’ of ‘file2.txt’ remains).
* When copying individual files, the usual practice is to use the Copy command.
* If ‘echo D’ is used instead of ‘echo F’, Xcopy creates a directory named ‘file2.txt’ and copies ‘file1.txt’ into that directory.
* If file2.txt exists, the prompt ‘Overwrite file2.txt (Yes/No/All)?’ instead of ‘Does file2.txt specify a file name or directory name on the target (F = file, D = directory)?’ is displayed (while the given input is only ‘F’), causing the above command fails. To force overwrite, /Y option must be used.
xcopy /U /S /E /Y Z:\Data\*.* E:\Current\Data\
[Windows 95 or later] Copy all files from ‘Z:\Data’ to ‘E:\Current\Data’, but do not copy if the file to be copied already exists in the destination and the destination file is newer. This is useful for situations where you only want to copy files when there are updates.
Sample 7 (Batch file)
@echo off setlocal enableextension for /F "tokens=1,2,* delims=:" %%i in ("%TIME%") do set MYTIME=%%i%%j set DIR_NAME=E:\Backup\src_%DATE:/=%_%MYTIME% if not exist "%DIR_NAME%" md "%DIR_NAME%" xcopy /S /E /Y /Q /EXCLUDE:"E:\Backup\excludes.txt" D:\Develop\src\*.* "%DIR_NAME%\"
[Windows NT series] [Extensions] Creates a directory in ‘E:\Backup’ with the date and time in its name, and within that directory, copies all files from ‘D:\Develop\src’, excluding those that match the patterns listed in ‘E:\Backup\excludes.txt’. Creating a batch file with such descriptions allows for the easy creation of backups at that particular point in time.
Note that characters like ‘/’ and ‘:’ cannot be used in filenames, so For and Set are used to remove these characters from the date and time.