Xcopy command : syntax and examples

by admin 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

{ 15 comments… read them below or add one }

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.

Reply

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,

Reply

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?

Reply

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′.

Reply

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.

Reply

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.
Thanks in advance.
I have windows XP 64 bit.
Tom

Reply

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

Reply

uche April 4, 2014 at 11:07 am

does it mean that it will not work in windows 8

Reply

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.

Reply

Fred April 21, 2014 at 7:49 pm

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

Leave a Comment

HTML tags are not allowed.

Previous post:

Next post: