Xcopy command : syntax and examples

xcopy is a built-in Windows command for copying files and directory trees. It can copy subdirectories, empty folders, hidden files, and only files changed since a date. This reference has been checked against the current Microsoft command documentation for Windows 11, Windows 10, and supported Windows Server releases.

XCOPY command syntax

xcopy <source> [destination] [options]

Put source and destination paths in quotation marks when either path contains spaces.

xcopy "C:\Source Folder" "D:\Backup Folder" /I

Copy files from one directory

xcopy "C:\Source" "D:\Destination" /I

This copies files in the source directory, but not files in its subdirectories. The /I option treats a destination that does not yet exist as a directory, avoiding the file-or-directory prompt in scripts.

Copy a directory and all subdirectories

xcopy "D:\data\documents" "E:\Newfolder\documents" /S /E /I

/S copies directories and subdirectories except empty ones. /E also copies empty subdirectories. Add /Y to suppress overwrite prompts when running unattended.

Advertisement

Exclude files or folders

Use /EXCLUDE with a text file that contains one search string per line. A matching string anywhere in the absolute path excludes that file.

type exclusions.txt
.pdf
.mp3

xcopy "D:\docs" "E:\newdocs" /S /E /I /EXCLUDE:exclusions.txt

For a more specific exclusion, include a distinctive folder or file-name fragment in the exclusion file. Test exclusions first with /L, which lists files that would be copied without copying them.

Copy hidden and system files

xcopy "D:\data\documents" "E:\Newfolder\documents" /S /E /H /I

/H includes hidden and system files. Use /Y as well when overwrite confirmation should be suppressed.

Copy files based on the archive attribute

xcopy "C:\Source" "D:\Backup" /A /S /I

/A copies files with the archive attribute set and leaves the attribute set. To clear the archive attribute after a successful copy, use /M instead.

xcopy "C:\Source" "D:\Backup" /M /S /I

Copy files changed on or after a date

xcopy "E:\data" "E:\backup" /D:02-01-2011 /I

Important: the documented date syntax is /D:MM-DD-YYYY. The example above copies files changed on or after February 1, 2011. Do not use DD-MM-YY: it can be interpreted incorrectly or rejected. If no date is supplied, /D copies source files newer than the files already at the destination.

Useful options for scripts

  • /L: list files that would be copied without copying them.
  • /F: display the source and destination file names.
  • /Z: copy network files in restartable mode.
  • /J: copy without buffering; useful for very large files.
  • /K: retain the read-only attribute on copied files.
  • /C: continue when an error occurs.

Supported Windows versions

Microsoft documents xcopy for current Windows 11 and Windows 10 releases and supported Windows Server releases, including Windows Server 2025, 2022, 2019, and 2016. Run xcopy /? on the target computer to see the options available there.

When to use Robocopy instead

For repeatable backups, large directory trees, or more resilient network copies, Robocopy command syntax and examples is often the better tool. Use xcopy when its simpler syntax and broad Windows compatibility fit the job.

Official reference

See Microsoft’s XCOPY command reference for the complete current option list and exit codes.

Advertisement

50 thoughts on “Xcopy command : syntax and examples”

  1. 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
  2. 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
  3. 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
  4. 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
    • source_directory and destination_directory need to be surrounded by quotation marks

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

  5. 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
    • You can use move and forfiles commands. Try below command.

      forfiles /S /M *.JPG /C "cmd /c move @file c:\folder"
  6. 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
  7. 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
    • 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

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

    Reply
  9. 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?

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

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

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

  10. 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
    Reply
    • 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
  11. 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

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

    Reply
  13. 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?

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

    Reply
  15. 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!

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

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

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

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

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

    Reply
    • yes, use notepad to type your xcopy command and save it as _name_.bat
      (when saving .bat file with notepad be sure to select all file types instead of .txt)

      then open windows admin tools menu and use open task scheduler
      add a new task that runs your .bat file and has a trigger scheduled for daily, 10pm

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

    Reply
  20. 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?

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

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

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

    Reply
  24. After having to learning all this, to sort a hidden problem that’s just appeared. Got the problem down to this after 3 hours. Worked with old system now just been used in new and doesn’t as the new system has changed the file types.
    So the question is
    Can you use it to exclude, using part of a file name. Can’t use file type to may files use the same type. Can’t use the full me as each on gets a new date stamp in the file name. Both are automatic and can’t change.

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

    Reply

Leave a Comment