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 is not an existing directory and could be a file or directory (ie. does not end with a backslash (\)), you are prompted to specify whether destination is a file or directory.
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.
/TCopies 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.
/HIncludes files with hidden and system attributes set.
/KCopies files without resetting the read-only attribute, if set.
/ROverwrites read-only files.
/UOnly 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 also prompts for instructions if there is ambiguity whether destination is a directory or file. An ambiguity is seen when destination does not refer to an existing directory or end in a " \ ". Note that a destination of 'name.ext' could be either a file or a directory.
/IForces 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.
/FThe /F switch is specified as "Displays full source and destination file names while copying" but actually does nothing.
/QDoes not display file names or any other messages while copying.
/CContinues copying even if errors occur.
Miscellaneous
/LDisplays files to be copied but does not actually copy them.
/NCopy 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).

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

  4. Unfortunately, the /U switch (to only copy files that already exist in the destination directory) has a rather annoying bug in that, when used with the /S switch (to include all subdirectories), the whole subdirectory tree is copied even if no files are found to copy. If trying to update files on a floppy with files scattered over the C: drive, the directory structure could well fill the floppy before actually getting to the files to be copied.

  5. 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 in the newsgroups 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. Actually, although I certainly don't advocate the practice, it seems that everything works just fine if xcopy.exe and xcopy32.exe are deleted and xcopy32.mod is renamed as xcopy.exe.

    If anyone understands the background/reasons for this name dancing, I would be most appreciative of their advice!

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.

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 diffuculties, 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.

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


If you should have any comments or suggestions,
please contact: Bob Watson
.
This page last revised:
August 29, 2000.