XCOPY command : Using the /EXCLUDE flag

   Posted November 7, 2007 by Jimmy Selix in Batch file programming

This recipe will briefly explain how to use the /EXCLUDE flag with XCOPY. This can be very handy if you need to backup a full directory but do not need or want specific file types or folder or file names.

XCOPY is a command that is present in XP’s command prompt.

XCOPY allows one to copy both files and directories in one command vs copying a folder or files in a folder one at a time using the COPY command. COPY does not allow you to copy subdirectories w/the *.*

Here is an example of when would use XCOPY vs COPY:

I have a folder on my C: drive called DATA
and under this folder i have a ton of text files (.txt) and also 3 folders called 1 2 and 3 which also have .txt files in them.

However, in the folders there are files with the name SECRET on them that i do not want copied.

I want to then copy this data w/the folder structure intact to a folder called BKUP on my C: drive.

Here is how we would use XCOPY and the /EXCLUDE tag to do this:

1. either open command prompt up or create new text file (if you plan to make .bat file). we will do a new text file and in turn make it into a batch file. after we create the new text file, rename it to test.bat for now.

2. right click the test.bat file and choose to Edit or Open With > Notepad.

3. we will add this command (i will explain each flag used)

XCOPY C:\DATA\*.* C:\BKUP /S /I /Y /EXCLUDE:c:\excludelist.txt

the /S flag = copies all directories and subdirectories except empty ones (use the /E instead if you want empty folders also copied)

the /I flag = If the destination does not exist and we are copying more than one file, it will assume the destination is a folder

the /Y flag = Suppresses all prompts to overwrite destination file if it already exists

the /EXCLUDE flag = this will point to a file that has the excluded file types or folder or file names. in my example, i created a text file called: excludelist.txt in the root of my C: drive.

in that excludelist.txt, i then added the word SECRET on the first line. If you have more than one excluded item; be sure to put each on a new line.

So once i run this batch file, it will look at the excludelist.txt file and then exclude any thing that contains the terms i put in the file from being copied (so none of my files w/SECRET in the name will be copied).

The main thing to remember when using the /EXCLUDE flag is that you have to put the location of the excluded file list after the flag; not the actual excluded items/terms. That initially was the most confusing part. You can create multiple excluded file lists; you would need to use the + list (ie XCOPY c:\* v:\ /EXCLUDE:c:\excluded1.txt+c:\excluded2.txt )


About Jimmy Selix

Jimmy Selix is an early adopter that loves to be one of the first on the block to have the latest and greatest in technology and gadgets. Another love of his is being able to share his knowledge to others seeking it. Feel free to drop any comments or questions that you may have.
View more articles by Jimmy Selix

The Conversation

Follow the reactions below and share your own thoughts.

  • Mustafa

    very nice, I didn’t try it yet, but I’m sure in I will in future . .

  • 13eastie

    Okay, but how do you go about using a an exclude file whose absolute path contains spaces e.g. one that is located in the “My Documents” tree?

    • Joe Webb

      Just guessing here but if you can’t quote the file path, i.e., “My Documents”, then you should at least be able to use the 8dot3 equivalent. (MYDOCU~1).

  • DizzyGoldfish

    Too bad I tried excluding each file extension within the command before reading your post 🙂

    Thanks for the help!

    • Steve

      You need to convert the path to short (i.e. DOS 8.3) format. then it will work OK.

      e.g. “My Documents” becomes MYDOCU~1

  • JohnG

    Any clue how to exclude a folder from being copied? Say for instance I have folder “X” on my desktop, but don’t want it or its contents copied when I run my batch backup that copies everything else on my desktop. Can’t seem to find anything regarding this.

    • Archarios

      xcopy source dest /exclude:list.txt

      contents of ‘list.txt’:
      X –for an exact directory match
      X –if directory begins with ‘X’
      X –if ends with ‘X’
      X –for anything (file/directory) containing ‘X’

      I’m still unsure as to wildcards tho

    • Brandon

      > Put this in your “C:\excludelist.txt” file on its own line:


  • kledi

    Thank a lot

    Its very clear

  • Named

    I finally know how to use /exclude, thanks a lot!

  • fantomaster

    Never having had to use /Exclude, I found the documentation at MS and elsewhere pretty confusing because no mention was made explicitly of having to define the list in a separate file. This article was very helpful in setting it right – thanks a lot!

  • Duhgorski

    best explaint on the web

  • upthereinthesky

    Wow. Thank you very much. This page is the most informative I have found on the web! I was lost without your help. Thanks again!

  • Auldy66

    good tip, cheers bruv 🙂

  • Yamin Rumon

    Thank you. Its working…………..

  • juan luis

    hola que tal estoy buscando la forma en como utilizar el Exclude con xcopy ya que lo tengo que implementar, la forma en que lo estoy utilizando es d la siguiente
    xcopy %ruta%\*.mp3 /S /C /Y /L /Q /EXCLUDE: c:\NOCOPIAR.txt >> BUSQUEDA.txt

    en NOCOPIAR TENGO la extencion .mp3, mi duda, que debe de ir dentro del archivo .txt, si es que no quiero que copie una carpeta que contenga archivos mp3, no se si me explique…

  • Patrick Oliveras

    this is great, thanks!