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 been trying to copy all files in the various sub-directories that have a JPG extension located on C: into a single directory on D:. I don't want to preserve the directory structure.

This is as far as I have gotten:

for /r C:\ %f in (*.jpg) do @copy "%f" D:\pictures

After this point, I'm asked if I want to overwrite D:\pictures. If I answer yes, I wind up with a single file, and if I answer no, no files get copied.

I also tried adding /y at the end of the command, but it just overwrites the preceding files leaving only the last one.

share|improve this question

This should work:

for /r C:\ %f in (*.jpg) do @copy "%f" D:\pictures\


The command copy "%f" D:\pictures copies the file %f to the location D:\pictures, which is the file pictures in the directory D:\.

Adding a trailing \ to make sure copy treats pictures as a directory.

If you copy multiple files to a single location (e. g., copy *.* D:\backup), copy automatically treats backup as a directory. But this is not the case when you copy a single file.

share|improve this answer
That did it! Thank you! – Doug Apr 14 '12 at 1:14
What to do if there is more than 1 file with the same name at different directories? Would the above command overwrite the fist copied file with the last one? How can we prevent this? Thanks. – NoChance Sep 3 '13 at 23:36
@EmmadKareem: None of the standard copy utilities (copy, xcopy, robocopy) have a built-in function to prevent clobbering. You should ask a new question. – Dennis Sep 5 '13 at 18:34

for /R c:\ %%1 in ("*.txt") do @xcopy "%%1" d:\txttemp%%~p1 /e/i/s/q/h

this code will copy without over write and will create folders and sub folders

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.