# Xcopy command : syntax and examples

by on February 19, 2011

What’s Xcopy?

Xcopy is a built in command on Windows OS.  Copy is the other command we have on Windows, it can copy files from one directory to another. But it does not work for copying directories.  Using xcopy we can copy directories also.

Copy all the files in one directory to another directory.

Xcopy /I Source_Directory  Destination_directory

This would copy only the immediate files in the source directory to the destination. It would not copy files from sub directories. Adding /I switch will avoid the question to the user ‘Does the destination directory specify a file name or directory name on the target‘.

Copy a directory including all its files and sub directories to another location on the same drive or a different drive.

Xcopy  /S /I /E Source_Directory  Destination_directory

For example to copy all the files from D:\data\documents to the folder E:\Newfolder\documents we need to run the below command.

Xcopy /S /I /E D:\data\documents E:\Newfolder\documents

This command creates the folder E:\Newfolder\documents if it already does not exist. It also creates the same directory/file structure in the destination folder. If the destination folder already exists you do not need to add /I switch. If the folder does not exist and you do not specify /I you will be prompted to confirm if the destination is a filer or folder. This would cause issues if you want to run unattended or automated copying of files.

Copy files including hidden and system files.

The above command excludes hidden and system files from copying. If you want to these files also then you need to add /H option to your command. Then the command would be

Xcopy /S /I /E /H D:\data\documents E:\Newfolder\documents

/E option causes to copy empty sub directories to the destination folder.

If you add /H option the the command would also copy hidden and system files to the destination folder.

If you are trying to overwrite an existing file then add the option /Y so that you will not be prompted for confirmation for overwriting the file.

Copy files based on archive attribute

If you want to copy only the files that have archive attribute set, you can use /A and /M switches. Archive attribute indicates whether the file has been modified since the time the attribute was reset.
The command for this is:

Xcopy /A /I /S source_directory destination_directory.

The above command keeps the archive attribute set; It does not reset the attribute.

If you want to reset the archive attribute, you can use /M switch instead of /A. The command for this case would be:

Xcopy /M /I /S source_directory  destination_directory

Exclude files in the copying
If you want to copy a directory to another location but want to exclude some files then you can use /EXCLUDE switch with Xcopy command.  You need to put the list of the files to be excluded in a file and then specify this file with the /EXCLUDE switch.

Xcopy /I Sourcedir  Destdir /EXCLUDE:filename

Example:
Copy the directory D:\docs to E:\newdocs\ excluding all pdf and mp3 files:

C:\>type 1.txt
.pdf
.mp3
C:\>Xcopy D:\docs E:\newdocs /EXCLUDE:1.txt

You can also specify the full names of the files. Each file name/pattern should be in a separate line.

The commands shown above can be used from batch files also. This command is available in Windows 7, XP, Vista, Server 2003 and Server 2008 editions.

Copy files based on modified date

Xcopy /D:dd-mm-yy  /I  sourcedir destinationdir

Example: To copy all the files in the directory ‘E:data’ that are modified on or after 1 st February 2011 to the folder ‘E:\backup

Xcopy /D:01-02-11 /I E:\data E:\backup

Related Posts:
Backup files using xcopy command

Srini May 3, 2011 at 11:02 am

More examples would have been good for excluding files from copying. For example, If I need to exclude certain file types in xcopy, say .doc, How can I do it.

admin September 3, 2011 at 11:14 am

Thanks for the comment. Added examples for those cases also.

Riddle June 14, 2011 at 8:23 am

Does this work on all Windows releases? I am running Vista and can’t seem to have this command,

admin September 3, 2011 at 11:13 am

Yes, it works on XP, Vista and Windows 7.

Aust September 3, 2011 at 4:20 am

How do I copy files that have not been modified in the last 7 days. I mean without specifying any date. I need to use xcopy command in a batch file and whenever I run the batch file, it should copy only the files that have been modified in the last 7 days. Any suggestion for this?

Jonathan September 3, 2011 at 11:31 am

Anyway to specify the excluded file types as xcopy arguments rather than mentioning in a file? I am getting ‘Can’t find file’ error if I do ‘xcopy /exclude:.mp3’.

Bruce November 6, 2011 at 5:37 pm

I am getting the below error with xcopy.
Invalid number of parameters.
0 File(s) copied

Can you tell me which cases one would see this error.

Douglas July 17, 2014 at 9:31 pm

source_directory and destination_directory need to be surrounded by quotation marks

ie: xcopy “C:\directory\*.*” “D:\directory\”

tom graham December 3, 2013 at 2:27 am

I have a whole lot of .JPGfiles that are in different folders with other files.
I want to extract (or just copy) all the .JPG files into one folder without the other stuff.
How can I do this?
I have tried various xcopy command strings but I still get the containing folders and the other files in that folder.
I have windows XP 64 bit.
Tom

admin December 7, 2013 at 2:51 am

You can use move and forfiles commands. Try below command.

forfiles /S /M *.JPG /C "cmd /c move @file c:\folder"

JohnW December 29, 2013 at 1:58 pm

Good tip nearly always missed for xcopy:

xcopy does not like split directory/folder or file names, eg : Documents and Settings , and will give an invalid number of parameters’ error message –

Just put the name inside quotes: “Documents and Settings” – problem solved

uche April 4, 2014 at 11:07 am

does it mean that it will not work in windows 8

admin April 7, 2014 at 5:14 am

This command should work on Win8 also. The article was published pre-Win8.

Fred April 21, 2014 at 2:30 am

Is it possible to copy a file or group of files with a variable path to them? e.g., certain flash settings files are always stored in the same location, save one folder in the path which name is changed after cache is cleared and flash has been reloaded.

Fred April 21, 2014 at 7:49 pm

Never mind. PowerShell is *exactly* what I’m looking for. Cheers!

sandip December 9, 2014 at 4:07 am

Thanks a ton for these examples.
How to exclude files which starts with specific name?

admin December 9, 2014 at 6:02 am

You can do that with /EXCLUDE switch. Put the name in a file and pass that file name with /EXCLUDE switch. For example, if you want to exclude all files starting with ‘abc’ you would create a text file(say patterns.txt) and put the text “abc”(without quotes).
Next, run the command below.
Xcopy srcdir destdir /EXCLUDE:patterns.txt

Ken January 11, 2015 at 6:15 pm

I am trying to copy three directories and have three xcopy statements but it only copies the first one. How can I have multiple statements execute after it deletes all the old files? Thanks!

Bob UK February 11, 2015 at 6:06 pm

I’m trying to use these lines in a batch file running on a Win8.1 64 bit but it does not work.

echo.
if not exist \\BACKUP\Backup\Bob\""." goto nodrive
xcopy e:\Bob \\BACKUP\Backup\Bob /d /e /h /i /y
echo

I can see the remote Backup machine in the network but it is not found with the “if not exist” or xcopy command nor the dir c ommand on the command prompt. It works fine if I use it to copy updated files to a local disk/ Are these commands supported in 64 bit Win 8.1?

Bob UK February 11, 2015 at 6:15 pm

sorry should be
if not exist \\BACKUP\Backup\Bob\*.* goto nodrive
but this does not work in 64 bit Win8.1 to the remote \\BACKUP machine but works if I use a local drive.

James Tew April 17, 2015 at 8:33 am

try pointing the “if not exist” at a specific file within the target location instead of “*.*”

James Tew April 17, 2015 at 8:30 am

I would like to use a single xcopy command to copy files to two different locations. Is this possible?

admin April 29, 2015 at 4:47 am

Not sure why you can’t run xcopy twice, once for each of the locations. But apparently xcopy does not allow to specify two destination folders for the copy.

srikanta May 24, 2015 at 12:28 pm

I have a requirement where i need to exclude some specific file types in child directory rather than in root directory. How can I achieve it using xcopy command with the help of exclude switch ? Please suggest.
My arrangement is like below,

D:\Source
D:\Source\abc.rpt
D:\Source\Child\abc1.rpt
D:\Source\Child\xyz.rpt
D:\Source\Child\def.txt

D:\Destination

I have used xcopy command as below

xcopy D:\Source D:\Destination /S /Y /Exclude:D:\Source\ExclusionList.txt

ExclusionList.txt has below exclusion list
————————————————–

child\.rpt

After executing xcopy command everything is copied from source to destination directory.
I want the output in destination directory as below

D:\Destination\abc.rpt
D:\Destination\def.txt

admin May 26, 2015 at 2:46 am

There does not seem to be any straight solution using xcopy. But one workaround is to mark all those files which should be excluded as hidden files. Xcopy by default would ignore hidden files from copying. So you can run the below commands.

 attrib +H child\*.rpt

and then

xcopy D:\Source D:\Destination /S /Y`

xcopy June 29, 2015 at 2:07 pm

Hello,
I have i bit problem to use xcopy with variable, when i put source and destinaton in variable the don’t work , without variable all work as exepected
Could y get an eye and see the mistake please.
echo off
SET SRC = C:\Projects\\source
SET DEST = C:\MBE\Projects\TESTLIVREAISON\destination
SET EXCLUDEDFILES = C:\MBE\Projects\TESTLIVREAISON\excludedfiles.txt

XCOPY %SRC% %DEST% /E /F /Y /EXCLUDE:%EXCLUDEDFILES%
pause

jann July 2, 2015 at 1:53 pm

Hello,

I would like to copy a set of files listed in a text file in the same folder structure to another location. Could you please help me on this.

keepthesefiles.txt has the list of files that need to be copied (A.txt)

src : folder1\folder2\A.txt
destination folder should look like this : folder1\folder2\A.txt

Thanks,
Jann

Yatin August 1, 2015 at 1:18 pm

Hi ,

How can i copy files and folder from one drive to another drive scheduled basis and i also want that when job will run next scheduled time it will only copy the NEW data (folder and file) from the source to destination?

satendra August 20, 2015 at 10:32 am

Dear all,

I have a question.
If i have some folders as well as some computers with different host names.
example:
computer names like: ABC-PC, DEF-PC, GHI-PC, JKL-PC, QRS-PC, XYZ-PC.
Folder names like: ABC_abc, DEF_def, GHI_ghi, JKL_jkl, QRS_qrs, XYZ_xyz.

is it possible to make a batch file which copy only that folder, which is according to the computer name.
if yes then what will be the command.

zaboykie October 25, 2015 at 8:23 am

Hi.
For regular backups, can I run a command that will copy all updated and new files in all directories and sub-directories to my backup drive, as opposed to copying everything as I’m doing now (takes forever)?
Looking forward to responses. Thank, guys!

Sameer November 6, 2015 at 11:11 pm

Hello,

I want to use xcopy tool to copy all drivers that are located in different folders to a one single folder in the destination. E.g. I have drivers extracted in a few folders with different file extensions and now I want to copy only files from those folders to the destination in ONE FOLDER, is this possible?