XCOPY

(from Extended Copy)

Copies files and directory structures. This is a more advanced version of the COPYCOPY command, but less advanced than the ROBOCOPYROBOCOPY command.

XCOPY file/directory [destination] [/A | /M] [/D[:date]] [/P] [/S [/E]] [/V] [/W] [/C] [/I] [/-I] [/Q | /F] [/G] [/L] [/H] [/R] [/T [/E]] [/U] [/K] [/N] [/O] [/X] [/Y] [/-Y] [/Z] [/B] [/J] [/EXCLUDE:str[+...]] [/COMPRESS] [/SPARSE]

Where:

  • file/directory - Specifies a file or directory. If a file is specified, it will be copied; if a directory is specified, the directory structure will be copied.
  • destination - Specifies which directory to copy the specified files or directories to (the new name can be specified if a file is specified).
  • /A - Copies only files with archive attribute (see ATTRIBATTRIB).
  • /M - Copies only files with archive attribute but removes it in the destination.
  • /D:date - Copies only files newer than the specified date (dd-mm-yy, depends on locale). If date is not specified, only files with a source timestamp newer than the destination timestamp will be copied.
  • /P - Asks for confirmation before copying each file.
  • /S [/E] - In addition to files, copies directories and subdirectories except empty ones. If /E is specified, copies directories and subdirectories including empty ones.
  • /V - Verifies each new file.
  • /W - Asks you to press a key before copying.
  • /C - Continues copying even if errors occur.
  • /I - If the destination does not exist and the copy involves multiple files, it assumes that the destination must be a directory.
  • /-I - If the destination does not exist and the copy is for a single file, it assumes that the destination must be a file.
  • /Q - Does not display statistics.
  • /F - Displays statistics.
  • /G - Allows you to copy encrypted files to a destination that does not support encryption.
  • /L - Logs copied files.
  • /H - Also copies hidden and/or system files.
  • /R - Overwrites read-only files.
  • /T [/E] - Copies the directory structure but does not copy files and empty directories. If /E is also specified, it copies the directory structure (including empty directories) but not files.
  • /U - Copies only files that already exist in the destination.
  • /K - Also copies attributes (the default is not to copy attributes except read-only ones).
  • /N - Copies using short names for created files.
  • /O - Also copies ownership information and ACLs (see ICACLSICACLS).
  • /X - Also copies the event control settings (implies /O).
  • /Y - Does not require confirmation to overwrite a file.
  • /-Y - Cancels /Y.
  • /Z - Copies files from network in restartable mode.
  • /B - Copies the symbolic link instead of the file it refers to.
  • /J - Copies using unbuffered I/O. Recommended for very large files to save RAM while copying.
  • /EXCLUDE:str[+...] Specifies that files and directories containing the specified string in their path or file name should not be copied. Multiple strings can be concatenated using the + symbol.
  • /COMPRESS - Requires network compression during file transfer (if applicable).
  • /SPARSE - Preserves the sparse attribute when copying a sparse file.

By setting the COPYCMD environment variable you can specify /Y as the default option, which can be disabled using /-Y, for example (see FORFOR and SETSET):

rem Create pippone.txt file and copy it in the directory "what a pair of balls"
echo super rant > epic_rant.txt
for /l %%n in (1,1,1000) do @echo super rant >> epic_rant.txt
xcopy epic_rant.txt "what a pair of balls\epic_rant.txt" /-i
rem Copy file again, asking for overwrite confirmation
xcopy epic_rant.txt "what a pair of balls\epic_rant.txt" /-i
rem Set COPYCMD
set copycmd=/y
rem Copy the file again, without asking for overwriting confirmation
xcopy epic_rant.txt "what a pair of balls\epic_rant.txt" /-i
rem Copy the file again, asking for overwriting confirmation again
xcopy epic_rant.txt "what a pair of balls\epic_rant.txt" /-i /-y

Example: Copy files and subdirectories (including empty ones) contained in the C:\Lorenzo directory to the C:\Personal Data directory, but only those more recent than January 1, 2000 that do not contain the strings lol and xd in the path or file name:

xcopy "C:\Lorenzo" "C:\Personal Data" /d:01-01-00 /s /e /exclude:lol+xd

Comments