Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a single-line script that copies all ".ext1" files from current folder to destination folder recursively:

xcopy *.ext1 D:\dest /s /d /y

It works perfectly.

Now I want to copy all *.ext2 as well. Instead of copy-pasting (that works fine):

xcopy *.ext1 D:\dest /s /d /y
xcopy *.ext2 D:\dest /s /d /y

I want to use a for command:

for %%f in (*.ext1 *.ext2) do xcopy %%f D:\dest /s /d /y

but it does not work:

  1. It does not copy subdirs of source folder
  2. It does not copy files with names with spaces like "read me.ext1"

I tried to use a /R key:

for /R %%f in (*.ext1 *.ext2) do xcopy %%f D:\dest /s /d /y

but it resulted in copying subdir source files into the root folder of dest. And names with spaces did not go.

What is a correct form of the for command would be?

share|improve this question

migrated from May 5 '13 at 11:50

This question came from our site for professional and enthusiast programmers.

for use with filenames with spaces use ...xcopy "%%f%" ... – Stephan May 5 '13 at 10:48
up vote 4 down vote accepted

try this:

for %%f in (.ext1 .ext2) do xcopy *%%f D:\dest /s /d /y
share|improve this answer

As far as I can tell your only option is to call xcopy multiple times. If you want to have some kind of code-reuse I see two options:

Define the paramters as variables and use them:

set dest=d:\dest
set options=/s /d /y

xcopy *.ext1 %dest% %options%
xcopy *.ext2 %dest% %options%

or create a sub-program to do the copying:

call :do_copy ext1
call :do_copy ext2

goto :eof

   xcopy *.%1 d:\dest /s /d /y
   goto :eof
share|improve this answer

You can't include wildcards in a regular for because they will always get expanded (or ignored if there is no match). If Endoro's answer doesn't work for you, you can read the list of wildcards from a file, or from the output of a command. Example:

for /f %%f in ('echo *.ext1 ^& echo *.ext2') do xcopy %%f D:\dest /s /y
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.