≡ Menu

Xcopy command : syntax and examples

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

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

C:\>type 1.txt
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

{ 34 comments… add one }
  • Srini

    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

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

  • Riddle

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

    • admin

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

  • Aust

    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

    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

    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

      source_directory and destination_directory need to be surrounded by quotation marks

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

  • tom graham

    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.

    • admin

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

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

    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

    does it mean that it will not work in windows 8

    • admin

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

  • Fred

    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

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

  • sandip

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

    • admin

      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

    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

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

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

    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

      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

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

  • James Tew

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

    • admin

      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

    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,


    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


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

    • admin

      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

    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


  • jann


    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


  • Yatin

    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

    Dear all,

    I have a question.
    If i have some folders as well as some computers with different host names.
    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.

    Thanks in advance.

  • zaboykie

    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


    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?

    Thanks in advance!

  • Neelam

    I want to copy a file from one http location to another, how to achieve it through xcopy. Currently I am doing this.
    xcopy http:\\abc.xyz\4.60.0-SNAPSHOT\ http:\\efg.hij\ /I /Y
    it says: Invalid path
    0 file(s) copied

    • Srini

      Xcopy won’t be able to copy files from wesbites(http). It can handle only the files on local disks or those shared over windows file sharing(SMB). Means only local and remote files which could be mapped to a drive. A website file can’t be mapped to a drive.

  • Please help i want to copy a directory called “New folder” using E:\>xcopy E:\New folder C:\New folder/O/X/E/H/K
    and i get this error: invalid number of parameters

Leave a Comment

Advertisment ad adsense adlogger