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.


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.

  • 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
  • 1
    @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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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