XCOPY usage - quickly copy files in Windows

“XCOPY” stands for Extended Copy, and was created to add more functions then the standard copy command. If you are using Windows, it should be installed already.

XCOPY is a command used on PC DOS, MS-DOS, OS/2, Microsoft Windows, and related operating systems for copying multiple files or entire directory trees from one directory to another and for copying files across a network. — Wikipedia

Xcopy copies multiple files and folders and can create the same structure to the copied location. (Something the copy command cannot do.)


xcopy Source [Destination] [/w] [/p] [/c] [/v] [/q] [/f] [/l] [/g] [/d[:mm-dd-yyyy]] [/u] [/i] [/s [/e]] [/t] [/k] [/r] [/h] [{/a|/m}] [/n] [/o] [/x] [/exclude:file1[+[file2]][+[file3]] [{/y|/-y}] [/z]

I am not going to go over each of the parameters, however I will provide some examples. If you need assistance with what all the parameters mean, just type xcopy /? in the command prompt and you should get a list.
I most often use XCOPY to copy all files and folders to a different location. XCOPY will automatically copy all directories and sub directories and recreate them in the [Destination].

xcopy /E "C:\Data\Howerd\TEXT\*.*" "O:\Data\Howerd\Text_All"

This should copy all files from the source to destination because the /E paramater – Copies directories and sub-directories, including empty ones.
The issue, however is, that XCOPY will ask you whether the file should be saved as a file (F) or directory (D), for every file from the [Source]! A message such as:

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

A workaround is:

echo f| xcopy /E "C:\Data\Howerd\TEXT\*.*" "O:\Data\Howerd\Text_All"  

This will copy all the folders and files in the source directory to the destination directory without prompting the user to enter F or D. “F” is selected for all the files.

Another neat feature of XCOPY is that it can create a Log txt file of all the data that it has copied/wasn’t able to copy. Adding >> [destination] to the end will create a log file and save it to the specified directory.

Example of Log file:

> echo f | xcopy /E /y "C:\Data\Howerd\TEXT\*.*" "O:\Data\Howerd\Text_All"  "C:\abclog.txt"

This will create a log file of everything being copied and save it to a text file in directory C:\abclog.txt

To update files in the Reports directory with the files in the Rawdata directory that have changed since October 30, 2013, type:

xcopy rawdata reports /d:10-30-2013

XCOPY is a very useful utility to copy files and folders while keeping their structure. It can be used in batch(.bat) file in conjunction with “if..then” statements to handle the errors and process the Exit codes. To utilize exit codes returned by XCOPY, use the errorlevel parameter on the if-statement in a batch program.
Exit codes are as follows:

  • 0 – Files copied without errors
  • 1 – No Files found to copy
  • 2 – User exited XCOPY by pressing [CTRL]+[C]
  • 4 – Initialization error; Not enough memory; Invalid syntax
  • 5 – Could not write to disk — [destination]

Batch file and XCOPY Exit codes:

@echo off 
rem COPYFILE.BAT transfers all files in all sub-directories 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 
echo Insufficient memory to copy files or 
echo invalid drive or command-line syntax. 
goto exit 
echo You pressed CTRL+C to end the copy operation. 
goto exit 

Copy and paste the code above to a file and name it COPYFILE.BAT. Now if you run the following command:

COPYFILE c:\mytext o:\copiedtext

The batch file will replace %1 with c:\mytext and %2 with c:\copiedtext and then uses XCOPY to start copying the files. If any of the errors described above occur, the corresponding message will be outputted to the console.

As you can see the there are a lot of possibilities with XCOPY. I have just covered the basics. The program has a lot of parameters available to make copying files easier.

Another utility which essentially does the same thing slightly differently is called Robocopy. Read about it is my Robocopy tutorial. Let me know which utility you prefer.

Leave a Reply

Your email address will not be published. Required fields are marked *