Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have been trying to copy all files in the various subdirectories 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

1 Answer 1

This should work:

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

Explanation:

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. –  Emmad Kareem 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

Your Answer

 
discard

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.