xcopy Command Tutorial

xcopy is a command used to copy files and folders in Windows operating systems. xcopy command provides a lot of useful features in order to copy specific file types, subdirectories, read-only files, etc.

xcopy Syntax and Parameters

xopy command has the following syntax where SOURCE and DESTINATION is important with PARAMETERS

xcopy SOURCE DESTINATION PARAMETER
  • SOURCE is the source files and folders we want to copy. SOURCE is required.
  • DESTINATION is the destination we want to copy to. DESTINATION is optional.
  • PARAMETERS are used to provide different options for xcopy commands like ignore errors, copy subdirectories.
ParameterDescription
/wPress any key to start copy
/pPrompt to copy each file and folder
/cIgnore errors
/vVerify each copied destination file
/qDo not display any output or message
/fDisplay source and destination filename
/lList files that will be copied
/uCopy files which already exist in the destination
/sCopy recursively subfolders and subdirectories except empty
/eCopy recursively subfolders and subdirectories including empty
/tCopy only directories or folder do not copy files
/rCopy read-only files
/hCopy hidden and system files
/oCopy file owner and DACL
/xCopy file audit settings and SACL
/ySuppress prompting and overwrite an existing destination file
/-yPrompt for every existing destination file
/?Display help information

xcopy Exit Codes

When the copy process is completed with the xcopy command it will return an exit code in order to provide information about the copy process. The following exit codes are returned.

Exit codeDescription
Files were copied successfully without any error.
1In the source, no files were found to copy.
2Copy interrupted when the user pressed CTRL+C.
4Initialization error occurred. There is not enough memory or disk space, or you entered an invalid drive name or invalid syntax on the command line.
5Disk write error occurred which may be related destination disk permission, or full.

Display xcopy Help

xcopy command provides a lot of parameters and options where help information about them can be listed with the /? option like below.

C:\>xcopy /?
Copies files and directory trees.

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] [/B] [/J]
                           [/EXCLUDE:file1[+file2][+file3]...]

  source       Specifies the file(s) to copy.
  destination  Specifies the location and/or name of new files.
  /A           Copies only files with the archive attribute set,
               doesn't change the attribute.
  /M           Copies only files with the archive attribute set,
               turns off the archive attribute.
  /D:m-d-y     Copies 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.
  /EXCLUDE:file1[+file2][+file3]...
               Specifies a list of files containing strings.  Each string
               should be in a separate line in the files.  When any of the
               strings match any part of the absolute path of the file to be
               copied, that file will be excluded from being copied.  For
               example, specifying a string like \obj\ or .obj will exclude
               all files underneath the directory obj or all files with the
               .obj extension respectively.
  /P           Prompts you before creating each destination file.
  /S           Copies directories and subdirectories except empty ones.
  /E           Copies directories and subdirectories, including empty ones.
               Same as /S /E. May be used to modify /T.
  /V           Verifies the size of each new file.
  /W           Prompts you to press a key before copying.
  /C           Continues copying even if errors occur.
  /I           If destination does not exist and copying more than one file,
               assumes that destination must be a directory.
  /Q           Does not display file names while copying.
  /F           Displays full source and destination file names while copying.
  /L           Displays files that would be copied.
  /G           Allows the copying of encrypted files to destination that does
               not support encryption.
  /H           Copies hidden and system files also.
  /R           Overwrites read-only files.
  /T           Creates directory structure, but does not copy files. Does not
               include empty directories or subdirectories. /T /E includes
               empty directories and subdirectories.
  /U           Copies only files that already exist in destination.
  /K           Copies attributes. Normal Xcopy will reset read-only attributes.
  /N           Copies using the generated short names.
  /O           Copies file ownership and ACL information.
  /X           Copies file audit settings (implies /O).
  /Y           Suppresses prompting to confirm you want to overwrite an
               existing destination file.
  /-Y          Causes prompting to confirm you want to overwrite an
               existing destination file.
  /Z           Copies networked files in restartable mode.
  /B           Copies the Symbolic Link itself versus the target of the link.
  /J           Copies using unbuffered I/O. Recommended for very large files.

The switch /Y may be preset in the COPYCMD environment variable.
This may be overridden with /-Y on the command line.

Copy File

You can copy a single file with the xcopy command easily by providing the source file and destination file. Below we will copy the file named test.py into backup.py . We can see that when we try to copy a file we will ask if the destination is a file or folder.

C:\Users\ismail\Desktop>xcopy test.py backup.py
Does backup.py specify a file name
or directory name on the target
(F = file, D = directory)? F
C:test.py
1 File(s) copied

Copy Folder

We can also copy a folder which contains some files. If the given folder do not contains any file it will not copied unless we provide the /e parameter.

C:\Users\ismail\Desktop>xcopy Test Backup
Does Backup specify a file name
or directory name on the target
(F = file, D = directory)? D
Test\backup.py
1 File(s) copied

Copy All Files and Folders From Source To Destination

By default xcopy do not copies files and folders recursively. It will only copy first level of files. We can copy files and folders recursively we can provide /e and /s options. /s option will copy recursively subfolders and files except empty ones where /e option copy recursively files and subfolders even empty ones.

> xcopy /s Test Backup 

> xcopy /e Test Backup 

Copy Hidden and System Files

By default xcopy do not copies hidden and systems files. But if we want to copy both system and hidden files we can provide the /h option like below.

> xcopy /s Test Backup 

> xcopy /e Test Backup 

Copy Only Changed Files or Update Only

A copy operation can create a load on the system and generally not all copied files are changed after the last copy. This is especially possible for taking periodic backups where only some files will be changed. We can use the /d option which will only copy non-existing or outdated or changed files on the destination.

> xcopy /d Test Backup 

Copy Only Changed Files or Update Only After Specified Date

We can also copy files changed after the specified date by using the /d option. We will also provide the date in the MM-DD-YYY. In the following example we will copy only files changed after 10-01-2020.

> xcopy /d:10-01-2020 Test Backup 

Only List Do Not Copy Files and Folders

By default the xcopy command files copy given source files and folder into the destination. But we can simulate the copy operation and do not actually copy and just print list files and folders which can be copied.

> xcopy /l Test Backup 

Copy To The Network or SMB File Share

xcopy command popularly used to copy files over the network. We can copy local or remote files over the network by specifying the remote system IP address or hostname.

> xcopy /e \\192.168.1.1\files C:\backup
READ  "ipconfig /flushdns" - Reset DNS Cache In Windows, MacOSX, and Linux

Leave a Comment