Xcopy

This command is used to copy files and directories, including subdirectories.

Syntax

xcopy source [destination] [/w] [/p] [/c] [/v] [/q] [/f] [/l] [/d[:date]] [/u] [/i]
[/s [/e]] [/t] [/k] [/r] [/h] [/a|/m] [/n] [/exclude:filename]

Parameters

source

Specifies the location and names of the files you want to copy. Source must include either a drive or a path.

destination

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.

/w

Displays a message asking you to press a key to begin copying files, and waits for your response before starting to copy files.

/p

Prompts you to confirm whether you want to create each destination file.

/c

Ignores errors.

/v

Verifies each file as it is written to the destination file to make sure that the destination files are identical to the source files. This switch is ignored because the functionality is inherent to the Windows 95 operating system. The switch is accepted only for compatibility with previous versions of MS-DOS.

/q

Suppresses display of xcopy messages.

/f

Displays source and destination filenames while copying.

/l

Does not copy files, only displays (lists) files that would be copied.

/d[:date]

Copies only source files changed on or after the specified date. If the date value is missing, xcopy copies all source files that are newer than the time of existing destination files. This option allows you to update only files that have changed. Notice that dates in the twenty-first century must be specified using four digits (for example, /D:1-1-2001 if m-d-y is the date format). That is, /D:1-1-01 is interpreted as 1 January 1901 rather than 1 January 2001.

/u

Copies (updates) only files from source that exist on destination.

/i

If source is a directory or contains wildcards, and destination does not exist, xcopy assumes destination specifies a directory name and creates a new directory and then copies all specified files into the new directory. By default, xcopy prompts you to specify whether destination is a file or directory.

/s

Copies directories and subdirectories, unless they are empty. If you omit this switch, xcopy works within a single directory.

/e

Copies all subdirectories, even if they are empty. Used with the /s and /t switches.

/t

Copies only subdirectory structure (tree), not files. To copy empty directories, you must include the /e switch.

/k

Copies files and retains the read-only attribute on destination files if present on the source files. By default, the read-only attribute is removed.

/r

Copies over read-only files.

/h

Copies files with the hidden and system file attributes. Xcopy will not copy hidden or system files by default.

/a

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 online Help for the attrib command.

/m

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 attributes, see the online Help for the attrib command.

/n

Copies using aliases (short file or directory names). This switch is required when copying files or directories from a VFAT volume to a FAT volume or when the 8.3 file naming convention is required on the destination volume.

/exclude:filename

Excludes the files listed in the specified file from the copy operation. The exclusion file can have a list of exclusion patterns (one per line, no wild card characters are supported). If any exclusion pattern in the file matches any part of the path of a subject file, that file is not copied.

Notes on Xcopy

Default value for destination.

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

Specifying whether the destination is a file or a directory.

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


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

You can avoid this prompt by using the /i switch, in which case xcopy assumes the destination is a directory if the source is more than one file or a directory.

Xcopy sets an 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 information, see the online Help for atttrib 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. 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.

Xcopy exit codes.

The following list shows each exit code and a brief description of its meaning:

0

Files were copied without error.

1

No files were found to copy.

2

The user pressed CTRL+C to terminate xcopy.

4

Initialization error occurred. There is not enough memory or disk space, or you entered an invalid drive name or invalid syntax on the command line.

5

Disk write error occurred.


You can use the errorlevel parameter on the if command line in a batch program to process exit codes returned by xcopy.

Xcopy Examples

To copy all the files and subdirectories (including any empty subdirectories) from the disk in drive A to the disk in drive B, type:


xcopy a: b: /s /e 

To include any system or hidden files in the previous example, add the /h switch when typing:


xcopy a: b: /s /e /h

To update files in the REPORTS directory with the files in the directory RAWDATA that have changed since December 29, 1993, type:


xcopy \rawdata \reports /d:12/29/93 

To update all the files that exist on \REPORTS in the previous example, regardless of date, type:


xcopy \rawdata \reports /u

To obtain only a list of the files that would be copied for the previous command, without copying the files, type:


xcopy \rawdata \reports /d:12/29/93 /l > xcopy.out

The file XCOPY.OUT lists every file that would be copied.

To copy the \CUSTOMER directory and all subdirectories, including empty directories, to the directory \PUBLIC\ADDRESS on network drive H and retain the read-only attribute, while being prompted when a new file is being created on H, type the following:


xcopy \customer h:\public\address /s /e /k /p 

To issue the previous command and ensure xcopy creates, without prompting, the directory \ADDRESS if it does not exist, add the /i switch:


xcopy \customer h:\public\address /s /e /k /p /i

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, the following batch program uses replaceable parameters for the xcopy source and destination parameters:


@echo off 
rem COPYIT.BAT transfers all files in all subdirectories of
rem the source drive or directory (%1) to the destination rem drive or directory (%2)
xcopy %1 %2 /s /e
if errorlevel 4 goto lowmemory
if errorlevel 2 goto abort
if errorlevel 0 goto exit
:lowmemory
echo Insufficient memory to copy files or
echo invalid drive or command-line syntax.
goto exit
:abort
echo You pressed CTRL+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 the following command:


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. Windows 95 displays the appropriate message and exits from the batch program.