XCopy

Index

Copies files and directory trees. XCOPY is similar to the COPY command except that it has many more switches that allow considerable control over exactly what is copied when using wildcards..

Syntax:

XCOPY source [destination] [/Switches]

Switches:
sourceThe file(s) to be copied. Although this must be a single parameter, it may include multiple files specified using wildcards (* or ?).
destinationThe location and/or name(s) of new files.
If destination is omitted, the files are copied to the current directory.
If destination refers to a directory that does not exist, the directory is created.
If destination could be either a file or directory, you are prompted for instructions. (See Notes below).
Scope: By default, XCOPY will confine its operation to files in the source directory.
/ECopies the complete subdirectory structure of source and all files therein.
/SCopies the complete subdirectory structure of source and all files therein but does not copy empty subdirectories.
/T WCopies 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.
Filters and Attributes: By default: XCOPY will not copy files which have 'system' or 'hidden' attributes set;
copies of 'read-only' files do not have the read-only attribute set, and
all copied files have their 'archive' attribute set. .
/AOnly copies files with the archive attribute set (ie. files that have not been "archived" using xcopy, msbackup, etc.).
/MOnly copies files with the archive attribute set, turning off the archive attribute of the source files.
/H WIncludes files with hidden and system attributes set. This switch does function in Dos mode when using the WindowsME version of XCOPY.
/K WCopies files without resetting the read-only attribute, if set.
/R WOverwrites read-only files.
/U WOnly copies files in source that already exist in destination. Can be used with /D to update files with more recent versions. This switch has a bit of a bug - see Note 5 below.
/D:dateOnly copies files with a 'last modified date' the same as, or later than, date.
If date is not specified, all files are copied except for same-named files in the destination directory that have a more recent 'last modified date' than the one in source.
Note that dates after 2000 must include the full year (01 is read as 1901, not 2001).
The format for date depends on the COUNTRY setting being used. (For US it is mm/dd/yy(yy))
PromptsBy default:
XCOPY prompts before overwriting files in destination with files in source of the same name (note: this default can be modified using the COPYCMD environment variable - see SET).
XCOPY prompts for instructions if there is ambiguity whether destination is a directory or file.
If an error occurs, an message is displayed and XCOPY quits.
/I WForces destination to be treated as a directory. The file/directory prompt is suppressed.
/YOverwrites existing files without prompting.
/-YPrompts before overwriting existing files.(This is the natural default, and overwrites any changes via the COPYCMD environmental variable
/WDisplays a prompt before starting to copy files.
/PPrompts for confirmation before creating each destination file.
/F WThe /F switch is specified as "Displays full source and destination file names while copying" but actually does nothing.
/Q WDoes not display file names or any other messages while copying.
/CContinues copying even if errors occur.
Miscellaneous
/L WDisplays files to be copied but does not actually copy them.
/N WCopy using the generated short names. This is necessary when copying from a VFAT volume to a FAT volume.
/VVerifies that new files are readable (does not compare with the original - see VERIFY). This switch is ignored when XCOPY is running in a Windows Dos box.
Footnote
WSwitch is valid when XCOPY is used in a Dos box under Win95/8/ME but not in Dos mode.

Notes:

  1. The /S and /E switches extend the utility of other switches to cover the files in the subdirectories of the source directory.

  2. XCOPY will not copy a folder's attributes (eg. 'hidden'). These have to be set as required using Windows Explorer or ATTRIB.

  3. If destination coincides with an existing directory name or concludes with a " \ ", it must be a directory. If destination includes a wildcard, it is assumed to be a file. Whilst the /I switch will force destination to be treated as a directory, there is no similar switch to force treatment as a file. (Note that a destination of "name.ext" could be either a file or a directory).

  4. The /C (don't stop for errors) switch can be useful when backing up a hard drive that includes the Windows swap file. XCOPY will not copy this file (because it is in use) but, without the switch, displays an error message and abandons the copying process. If the switch is included, the swap file is still not copied (it would be of no use in a backup anyway) but the rest of the drive can be copied successfully (but see Caveat below).

  5. If the /U and /S switches are used together (to update files in all subdirectories), files will only be updated if they are in the same relative position in the directory structure. This switch combination will not work to update (say) all "*.inf" files scattered over the C: drive to a backup floppy of "*.inf" files all on the root directory. In such a situation, if XCOPY found c:\windows\klop\setup.inf, it would look for a:\windows\klop\setup.inf and, not finding it, would move on. It would ignore a:\setup.inf.
    Note also that the directory structure of source will be copied even if no files are found to meet the /U switch specifications.

  6. If a directory contains files meeting the source specification, as far as XCOPY is concerned, files exist to be copied, even if switches are used that mean that no files are copied. So, if source is specified as "c:\*.*" but the /U switch is used and the destination disk has none of the files in the c: root directory, then XCOPY returns an error level of 0 ("files copied without errors"), even though no files were copied. However, if source were specified as "c:\*.junk" and there were no such files, XCOPY returns an error level of 1 ("no files found to copy").

  7. In the transition from Dos 6.x to Dos 7.x, XCOPY has been significantly enhanced. For reasons I do not understand, in Win 95 (all versions) the new program was not simply named "xcopy.exe" but "xcopy32.exe" and xcopy.exe became simply a pointer to xcopy32.exe. Xcopy32.exe could be invoked directly and would run just fine on its own.

    In spite of MS urging to NOT use the XCOPY32 command (rather than sticking with XCOPY) there have been persistant - totally inaccurate - reports to the effect that using XCOPY32 will work with long filenames when working in a boot-to-Dos environment.

    Perhaps in an effort to forestall this apparently harmless practice, in Win98 MS renamed the main file "xcopy32.mod" which is called automatically by both XCOPY and XCOPY32.

    In Win98, xcopy.exe and xcopy32.exe are identical and both simply point to xcopy32.mod. Xcopy.exe and xcopy32.mod must be in the same directory - (which they normally are: ie. c:\Windows\command).Actually, it seems that everything works just fine if xcopy.exe and xcopy32.exe are deleted and xcopy32.mod is renamed as xcopy.exe.

Caveat

Whilst XCOPY is usually perfectly sufficient for copying the contents of one disk to another and making backups, it cannot be considered 100% reliable. The problem arises because the relationship between long and short filenames is not necessarily the same in both original and copy and this can cause problems if a program calls a file by its short name.

Although this sounds like a serious flaw in the whole long/short filename concept and it is not difficult to demonstrate the potential difficulties, the fact is that in real life it is seldom that all the conditions for trouble are met simultaneously. To be specific, these conditions are:

  • A program or file must be called with its short filename and/or the path must include a short name.
  • There must be at least two versions of the short name.
  • At some time, one version of the short name (but not the latest) on the source must have been deleted.

This problem is discussed in some detail (and with evangelical zeal) at PCguide - Articles - Xcopy Xposed by Charles M. Kozierok and How do I clone/backup a hard disk under Windows? by John Navas.

Tricks

Creating directory trees

As mentioned above, if destination refers to a directory that doesn't exist, it is created. If destination refers to a directory tree that doesn't exist, the whole tree will be created. Furthermore, XCOPY will accept NUL as a source file (though this does cause a "The system cannot find the file specified" error message). It is thus possible to create a complete directory tree with a single XCOPY command:

XCOPY NUL drive:\dir1\subdir2\....\subdirn\ /i

This will create the empty directory tree to subdirn. By contrast, the MD command will only allow subdirectories to be created one at a time.

Creating file lists

While the /L switch (list files without actually copying them) is primarily intended to check that an XCOPY command is going to do what was intended before the command is actually carrried out, it can also be used to create file lists with different criteria (and output) to those available with DIR. To avoid "cyclic copy" error messages, the destination should be the root directory of a valid drive (C:\ is good) and to avoid the prompts for overwriting, the /y switch should be included.

Exit Codes:

0All files were copied without errors
1No files were found to copy
2XCOPY was terminated by Ctrl-C before copying was complete
4An initialization error occurred.
Such an error would generally caused by insufficient memory or disk space, or invalid drive name or syntax).
5 A disk-write error occurred.

Examples

  1. To copy all files and subdirectories from the data directory to the disk in drive a:
    xcopy c:\data a: /s
    or
    xcopy c:\data\*.* a: /s
  2. To copy all files and subdirectories from the data directory created/modified since 1st Jan.1997 to the disk in drive a: should be:
    xcopy c:\data a: /s /d:1/1/97
  3. To update any files, including those in subdirectories, on the disk in drive a: from everywhere in the data directory on c: should be:
    xcopy c: a: /s /u /d
    but will copy all subdirectories of c: to a: - in many cases not leaving much room for files!

File Details

File NameDefault LocationDos Ver.Win Ver.SizeDateSource
Xcopy.exec:\windows\command 7.0Win95 3 878111/07/95win95_09.cab
7.1Win95 (OSR2.x) 3 878224/08/96win95_14.cab
Win98 3 878311/05/98win98_43.cab
Win98 SE 3 878323/04/99win98_47.cab
Xcopy32.exec:\windows\command 7.0Win95 40 96011/07/95win95_09.cab
7.1Win95 (OSR2.x) 41 472424/08/96win95_14.cab
Win98 3 878311/05/98win98_43.cab
Win98 SE 3 878323/04/99win98_47.cab
Xcopy32.modc:\windows\command 7.1Win98 41 472511/05/98win98_45.cab
Win98 SE 41 472623/04/99win98_51.cab

Superscripts denote which same size files, if any, are identical (using FC).


This page last revised:
January 1, 2003.