2

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.

2 Answers 2

2

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.

2
  • 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
    Commented Sep 3, 2013 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
    Commented Sep 5, 2013 at 18:34
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .