≡ Menu

Xcopy command : syntax and examples

Xcopy command

Xcopy is a built in command on Windows OS which has advanced features than the basic Copy command. The additional features Xcopy has are listed below.

  1. Xcopy can copy directories
  2. Xcopy can copy all files including subdirectories recursively and can replicate the source directory structure as is.
  3. Xcopy can exclude files based on file name or extension
  4. Xcopy can help identify updated files(based on archive attribute or based on a given cutoff date), so it’s useful for incremental backup needs.

Xcopy command to copy files.

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 files and sub directories recursively.

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.

Xcopy – Exclude files

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.

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

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

Supported versions

Xcopy is available in all newer versions of Windows – Windows 10, 7 and 8. Particularly, I’ve verified it to be working on Windows 10.

Related Posts:
Backup files using xcopy command

48 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.
    Tom

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

    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

      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,

    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

      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

    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

    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

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

    Thanks in advance.

  • zaboykie

    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!

    • LezLezLez

      Hi, this is my problem too. I had the XCOPY command in a DOS BAT file under XP SP3 but have now migrated to Windows 7 (14-Jan-2017). I get message “Invalid Drive Specification” from bat line of “XCOPY c:\Users\G^&S e:\Users\G^&S /c /d /e /f /g /h /o /r /y” – can anyone see what is wrong please? Thanks in advance. Geoff.

  • Sameer

    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?

    Thanks in advance!
    Sameer

  • Neelam

    Hi,
    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.

  • dominic

    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

  • greg

    I wish to run xcopy every night at 10:00 pm
    is there a easy way to do that?
    thank you for your time

  • pete

    Hi, can someone please help with this –
    i have a list of thousands of files in various locations. I want to write a bat file to copy them to one target folder in one sweep.
    Does anyone know if that’s possible and if so what the syntax would be?

    Thanks.

  • Neppoliean J

    Could you please help me to backup my windows files to AWS Ec2 windows machine Using Xcopy command?

  • Aman Kumar

    I want to copy only the modified folders in source directory to Destination directory.
    Trying with below command:
    xcopy %sourceDir% %destDir% /s /d
    But it’s asking for conformation(Yes/No/All) for non-modified folders as well.
    I don’t want any manual intervention, just want to copy the modified folders.
    Could you please help?

  • Manas Dash

    Hi Aman,

    Use /Y switch in XCOPY to suppress asking for (Yes/No/All) every time.

  • Mark

    How can I run a script if the xcopy copies a file and if not exit without running the script?

  • Noya Business

    Stumbled on your site and thought I’d point out that your /D:dd-mm-yy information is incorrect and could mislead someone. Correct syntax is Month first then day.

    Source: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/xcopy

    Thanks!

  • LynxBe

    Copy files based on modified date (Xcopy /D:01-02-11 /I E:\data E:\backup)
    doesn’t work on my copy of Windows 7:

    E:\>xcopy e:\DATA g:\francine-old\data /D:23-04-19 /E /L
    Paramètre non valide – /D:23-04-19 (INVALID PARAMETER)
    E:\>xcopy . g:\francine-old\data /D: /E /L
    Commutateur non valide – /D: (INVALID SWITCH)

    Any help ?
    TIA

  • Ann Mariani

    I wish to copy files excluding duplicates and corrupted files.

  • Mitul

    Hi,
    xcopy /d /S /E /y /C /q c:\_git c:\backup /EXCLUDE:c:\_git\Deployment\exclusions.txt

    this command is working as expected. BUT i have an issue with exclusions.
    i have \Documents\ as part of exclusion from base dir and i have documents folder under different sub directory too. but xcopy is not copying the other document folder which i think it should copy.
    api\Documents ( i am expecting this to exclude and it excludes it)
    but
    api\bin\directives\documents ( i am not expecting this to exclude but the command is excluding it)
    question: how do i exclude one Documents folder while copy the other one.

    my exclude file contains \Documents\ ( relative path from api folder)

    i tried absolute path but its not working as i want.

    1) is there a way to make the folder name case sensitive during the exclusion?

    Mitul

  • Carnak

    how to XCOPY files WITHOUT COPYING THE SOURCE SUBDIRECTORIES !! I want all files in *one directory* on the destination; just a bucket for photo backup! HOW TO SUPPRESS DIRECTORY COPYING IN XCOPY OR ROBOCOPY.

    PLEASE. I am so sick of MS I could PUKE.

  • Muhammad Nas Hashemi

    I want to copy a specific type of file like .jpeg from all drives to my usb.

    How can I do it?

    Thanks in advance

  • Henry Das

    I want to include two kinds of file types. It works with one like *.jpg. But how do I list another one?

Leave a Comment