Home > Commands N-Z > Commands X

XCOPY


Description | Syntax | Parameters | Switches | Related | Notes | Examples | Errorlevels | Availability

Copies directories, their subdirectories, and files (except hidden and system files).

With this command, you can copy all the files in a directory, including the files in the subdirectories of that directory.


Syntax

XCOPY /?

XCOPY source [destination] [/Y | /-Y] [/A | /M] [/C] [/D[:date]] [/EXCLUDE:file1[+file2][+file3]...] [/F] [/G] [/H] [/I] [/K] [/L] [/N] [/O [/X]] [/P] [/Q] [/R] [/S [/E]] [/T] [/U] [/V] [/W] [/Z]


Parameters
destination (v3.2 Win95 NT3.5)
Specifies the destination of the files you want to copy. Destination can include a drive letter and colon, a directory name, a filename, or a combination. Will accept UNC pathnames.
source (v3.2 Win95 NT3.5)
Specifies the location and names of the files you want to copy. Source must include either a drive or a path. Will accept UNC pathnames.

Switches
/? (NTXP)
Displays help.
/A (v3.2 Win95 NT3.5)
Copies only source files that have their archive file attributes set. This switch does not modify the archive file attribute of the source file. For information about how to set the archive file attribute, see the ATTRIB command.
/C (v7.00 Win95 NT3.5)
Continue copying even when errors occur.
/D[:date] (v3.2 Win95 NT3.5)
Copies only source files modified on or after the specified date. Note that the format of date depends on the COUNTRY setting you are using. Dates after 2000 must use 4-digit year.
If no date is given, the default is to copy files with a modification date before today (at least 1 day before).
CAUTION: results may be unpredictable if used with /E or /S.
/E (v3.2 Win95 NT3.5)
Copies any subdirectories, even if they are empty.
/EXCLUDE:file1[+file2][+file3]... (NT3.5)
The files can each contain one or more full or partial pathnames to be excluded. When any of these match any part of the absolute path of a SOURCE file, then that file will be excluded.
/F (v7.00 Win95 NT3.5)
Does nothing. Supposed to "Display full source and destination file names while copying".
/G (NTXP)
Allows he copying of encrypted files to destination that do not support encryption.
/H (v7.00 Win95 NT3.5)
Copies source files that have their hidden and system file attributes set. For information about how to set the archive file attribute, see the ATTRIB command.
/I (v7.00 Win95 NT3.5)
Treats destination as directory (suppresses file/directory prompt).
/K (v7.00 Win95 NT3.5)
Copies source files without resetting read-only file attribute, if set. For information about how to set the archive file attribute, see the ATTRIB command.
/L (v7.00 Win95 NT3.5)
Displays source files to be copied, without copying them.
/M (v3.2 Win95 NT3.5)
Copies source files that have their archive file attributes set. Unlike the /A switch, the /M switch turns off archive file attributes in the files specified in source. For information about how to set the archive file attribute, see the ATTRIB command.
/N (v7.00 Win95 NT3.5)
Copy source files using generated short (8dot3) names. This is necessary when copying between disks that are formatted differently e.g NTFS and VFAT, or when archiving data to an ISO9660 CDROM.
/O (NT2000)
Copy file Ownership and ACL information.
/P (v3.2 Win95 NT3.5)
Prompts you to confirm whether you want to create each destination file.
/Q (v7.00 Win95 NT3.5)
Do not display filenames or error messages while copying.
/R (v7.00 Win95 NT3.5)
Overwrites read-only files.
/S (v3.2 Win95 NT3.5)
Copies directories and subdirectories, unless they are empty. If you omit this switch, XCOPY works within a single directory.
/T (v7.00 Win95 NT3.5)
Copies the subdirectory structure of source but does not copy any files and does not copy empty subdirectories. To include empty subdirectories, use with the /E switch.
/U (v7.00 Win95 NT3.5)
Only copies file(s) from the source that already exist in destination. When used with /E switch the WHOLE subdirectory tree is copied EVEN IF NO FILES ARE FOUND to copy.
/V (v3.2)
Verifies each file as it is written to the destination file to make sure that the destination files are identical to the source files.
CAUTION: results may be unpredictable if used with /E or /S.
Windows XP does not use this command. It is accepted only for compatibility with MS-DOS files.
/W (v3.2 Win95 NT3.5)
Displays the message and waits for your response before starting to copy files:
        Press any key to begin copying file(s)
/X (NT2000)
Copy file audit settings (implies /O).
/Y (v6.2 Win95 NT2000)
Indicates that you want XCOPY to replace existing file(s) without prompting you for confirmation. By default, if you specify an existing file as the destination file, XCOPY will ask you if you want to overwrite the existing file. (Previous Versions of the DOS operating system would simply replace the existing file.) If the XCOPY command is part of a batch file, XCOPY will behave as in previous Versions. Specifying this switch overrides all defaults and the current setting of the %COPYCMD% environment variable.
/-Y (v6.2 Win95 NT2000)
Indicates that you want XCOPY to prompt you for confirmation when replacing an existing file. Specifying this switch overrides all defaults and the current setting of the %COPYCMD% environment variable.
/Z (NT4)
Copy files in restartable mode. If the copy is interrupted part way through, it will restart if possible (use on slow networks).

Related

For information about copying individual files, see the COPY command. For information about copying disks, see the DISKCOPY command.
DEL - Delete files.
MOVE - Move a file from one folder to another.
ROBOCOPY - Robust File and Folder Copy.
Equivalent Linux BASH commands:
cp - Copy one or more files to another location.
install - Copy files and set attributes.


Notes
Default value for destination

If you omit destination, the XCOPY command copies the files to the current directory.


Specifying whether destination is a file or directory

If destination does not contain an existing directory and does not end with a backslash (\), XCOPY prompts you with a message in the format:

    Does destination specify a file name
    or directory name on the target
    (F = file, D = directory)?

Press F if you want the file(s) to be copied to a file. Press D if you want the file(s) to be copied to a directory.


Setting the %COPYCMD% environment variable

You can set the COPYCMD environment variable to specify whether you want the COPY, MOVE, and XCOPY commands to prompt you for confirmation before overwriting a file in all cases, whether issued from the command prompt or from a batch file.

To force the COPY, MOVE, and XCOPY commands to prompt you before overwriting in all cases, set the %COPYCMD% environment variable /-Y. To force these commands to overwrite in all cases without prompting you, set the %COPYCMD% environment variable to /Y.

Typing any of these commands with the /Y or /-Y switch overrides all defaults and the current setting of the %COPYCMD% environment variable.


XCOPY does not copy hidden and system files

In older Versions of the DOS operating system, XCOPY copied hidden and system files. This is not the case in v6.22. To remove the hidden or system attribute from a file, use the ATTRIB command.


XCOPY sets archive attribute for destination files

XCOPY creates files with the archive attribute set, whether or not this attribute was set in the source file. For more information about file attributes, see the ATTRIB command.


XCOPY vs. DISKCOPY

If you have a disk that contains files in subdirectories and you want to copy it to a disk that has a different format, you should use the XCOPY command instead of DISKCOPY. Since the DISKCOPY command copies disks track by track, it requires that your source and destination disks have the same format. XCOPY has no such requirement. In general, use XCOPY unless you need a complete disk image copy. However, XCOPY will not copy hidden or system files such as IO.SYS and MSDOS.SYS. Therefore, use DISKCOPY to make copies of system disks.


Examples

This example copies all the files and subdirectories (including any empty subdirectories) from the disk in drive A to the disk in drive B:

    XCOPY A: B: /S /E

This example uses the /D: and /V switches:

    XCOPY A: B: /D:01/18/93 /S /V

In this example, only files on the disk in drive A that were written on or after 01/18/93 are copied to the disk in drive B. Once the files are written to the disk in drive B, the XCOPY command compares the files on the two disks to make sure they are the same.

You can create a batch program to perform XCOPY operations and use the batch IF command to process the exit code in case an error occurs. For example, this batch program uses replaceable parameters for the XCOPY source and destination parameters:

    @ECHO OFF
    REM COPYIT.BAT transfers all source
    REM files in all directories on the source
    REM drive (%1) to the destination drive (%2)
    XCOPY %1 %2 /S /E
    IF ERRORLEVEL 4 GOTO LOWMEM
    IF ERRORLEVEL 2 GOTO ABORT
    IF ERRORLEVEL 0 GOTO EXIT
    :LOWMEM
    ECHO Insufficient memory to copy files or
    ECHO invalid drive or command-line syntax.
    GOTO EXIT
    :ABORT
    ECHO You pressed CTRL+C (^C) to end the copy operation.
    GOTO EXIT
    :EXIT

To use this batch program to copy all files in the C:\PRGMCODE directory and its subdirectories to drive B, type:

    copyit c:\prgmcode b:

The command interpreter substitutes C:\PRGMCODE for %1 and B: for %2, then uses XCOPY with the /E and /S switches. If XCOPY encounters an error, the batch program reads the exit code and goes to the label indicated in the appropriate IF ERRORLEVEL statement. The operating system displays the appropriate message and exits from the batch program.


Errorlevels
Availability
External
DOS
v3.20 v3.05 v3.1 v3.21 v3.25 v3.30 v3.3A v3.3R v3.3T v3.31 v3.40 v4.0 v4.01 v4.01A v5.0 v5.0A v5.00.02 v5.001A v5.01 v5.02 v6.0 v6.10 v6.2 v6.21 v6.22 v6.23 v7.00 v7.0R1 v7.10 v8.00
Windows
Win95 Win98 WinME
Windows NT
NT3.5 NT4 NT2000 NTXP NT2003