Xcopy and its Application

The syntax and use of the command "Xcopy" is described. Of all the command line executables, Xcopy is one of the most useful for the average home PC user. It provides a powerful and versatile method for copying and backing up files and directories.

Syntax of Xcopy command

Xcopy has a large number of possible switches, which gives the command a great deal of flexibility. The syntax for the command is given by:

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

Upper case letters have been used above but the command is case-insensitive. A description of the various switches is given in Table I. Note that Windows Vista has an additional switch “/B”. The function of this switch is to copy a Symbolic Link itself instead of the target of the link. Windows 7 has added “/J” to use unbuffered I/O for large files.

Table I. Description of switches for the command Xcopy
SwitchDescription
/ACopies only files with the archive attribute set, doesn't change the attribute.
/MCopies only files with the archive attribute set, turns off the archive attribute. Useful in backup.
/D:m-d-yCopies files changed on or after the specified date. If no date is given, copies only those files whose source time is newer than the destination time. Useful in backup.
/PPrompts you before creating each destination file.
/SCopies directories and subdirectories except empty ones.
/ECopies directories and subdirectories, including empty ones. Same as /S /E. May be used to modify /T.
/VVerifies each new file. Not used by Windows XP.
/WPrompts you to press a key before copying.
/CContinues copying even if errors occur.
/IIf destination does not exist and copying more than one file, assumes that destination must be a directory.
/QDoes not display file names while copying.
/FDisplays full source and destination file names while copying.
/LDisplays files that would be copied.
/GAllows the copying of encrypted files to destination that does not support encryption.
/HCopies hidden and system files also.
/ROverwrites read-only files.
/TCreates directory structure, but does not copy files. Does not include empty directories or subdirectories.
/UCopies only files that already exist in destination.
/KCopies attributes. Normal Xcopy will reset read-only attributes.
/NCopies using the generated "short" names. May be necessary when copying from NTFS to FAT16.
/OCopies file ownership and ACL information.
/XCopies file audit settings (implies /O).
/YSuppresses prompting to confirm that you want to overwrite an existing destination file. May be preset in the COPYCMD environment variable
/-YPrompts to confirm you want to overwrite an existing destination file.
/ZCopies over a network in restartable mode.
/EXCLUDE:file1[+file2][+file3]...Specifies a list of files containing strings to be excluded. Tricky to use. See below.
/BCopies a Symbolic Link itself instesd of the target of the link. (Windows Vista/Windows 7)
/JCopies using unbuffered I/O. Recommended for very large files. (Windows 7)

Applications of Xcopy

Xcopy provides an excellent tool for backing up selected folders. With appropriate switches, a variety of backup scenarios can be created. One possible backup configuration would be to copy only those files that have been changed. Here is an example command: xcopy C:\somefolder E:\backupfolder /D /E /C /R /H /I /K /Y This command will copy all files, including those in sub-folders, that are newer in the source folder. It will copy hidden as well as read-only files and will create the destination folder and/or sub-folders if they do not already exist.

The next example shows the use of a wildcard; it collects all files of a given type and copies them into one place. The command xcopy C:\*.mp3 E:\mp3folder /S /I /C > E:\mp3List.txt will collect all MP3 files on the C: drive and copy them to a folder on the E: drive. It also creates a list of the files copied and places the list on the E: drive. This simple command preserves the sub-folder structure. If desired, a more advanced script could be written that places all MP3 files in one folder with no sub-folders.

Excluding files and folders

A useful feature of Xcopy is the ability to exclude certain files and/or folders from being copied by means of the “/Exclude” switch. All names containing a given string of characters can be excluded. Unfortunately, Microsoft’s description of the switch is not a model of clarity. In fact, postings on the Internet indicate considerable confusion exists about how to implement this handy feature. Perhaps the list below will help clarify how to use it.

  1. The strings contained in the names of the files and/or folders to be excluded from being copied are not entered directly in the command switch itself.
  2. The entry in the command switch is one or more text files that list the strings to be excluded.
  3. The format of the switch is /Exclude:{path}list_of_exclusions.txt Note the colon between Exclude and the name of the file that lists the exclusions. Although more than one exclusion file can be used, stick with one if you can.
  4. The name of the file that lists exclusions cannot have spaces. Quoting does not help.
  5. Be careful with pathnames. Remember that operations are relative to the working directory for the command prompt. I suggest placing the exclusion list in the top directory of the directories being copied and opening the command prompt in the directory just above the directory and sub-directories to be copied. (Go here if you don’t know how to open the command prompt wherever you want.) The various relative paths will then be simple. If you open the command prompt in the default location, things can get complicated.
  6. The structure of the exclusions list is one exclusion string per line. Wildcards are not used and do not work. If you want to exclude all files whose names contain ABC, simply place ABC on one line of the exclusion file. If it is being used together with the switch “/S”, the exclusion file will apply to sub-directories as well as the main directory.
  7. To exclude a particular sub-directory, place its name in the exclusion file with backward slashes before and after its name, viz., \excluded_directory\
  8. Use a text editor to create exclusion files. Do not use Microsoft Word or other word processor unless you are careful to create text files. Use ANSI encoding. When I tried UTF-8 encoding, the file did not work.

Exit codes for Xcopy

Many commands issue integer exit codes to indicate the status of the command after it is run. The exit codes for Xcopy are given in Table II. When placed in “If” statements, these error codes can be useful in batch files .

Table II. Exit codes for Xcopy
Exit codeDescription
0Files were copied without error.
1No files were found to copy.
2The user pressed Ctrl+C to terminate xcopy.
4Various errors including insufficient memory or disk space, an invalid drive name, or invalid syntax.
5Disk write error occurred.

Back to top

Nguồn: xcopy.htm (bản mirror trong original/)