How do you want it?
It isn't quite clear to me, how the result exactly should be -- should it be flattened or should it be hierarchical as well?
Look at this for example:
source
folder-1
folder-1-1
image1.jpg
folder-1-2
image2.jpg
cheese.jpg
image3.jpg
some_text.txt
folder-2
folder-2-1
image3.jpg
some_music.mp3
cheese.jpg
target
Should the result be basically a copy of the shown hierarchy (without any other file than the jpgs), or should it be a flattened result like this one:
source
... (see above)
target
image1.jpg
image2.jpg
cheese.jpg
image3.jpg
image3.jpg
How can you do it?
Flattened
You can use DOS' for
command to walk directories1 and make a custom function2 to handle the files:
@ECHO OFF
for /r %%f in (*.jpg) do call:copyFile %%f
GOTO END
:copyFile
copy /V /-Y %~1 ..\target
GOTO:EOF
:END
Meaning: for
every %%f
in the listing of *.jpg
from the current working dir, execute function copyFile
. The /r
switch makes the listing recursing (walk through all subdirectories).
In the function, the argument passed to it (now known as %~1
) is passed to the copy function: Copy the file to the target directory which is ..\target
in this case. /V
lets copy verify the result, /-Y
lets it ask for permission to overwrite files. See copy /?
!
Very big problem: If you have one or more files in different subfolders of your source directory, which have the same name (like the two cheese.jpg
s in my example), you will loose data!
So, I wouldn't recommend this approach, as you risk loosing data (digital cameras are not very creative in naming pictures!).
Hierarchical
Just use robocopy
:
robocopy /S <sourcedir> <targetdir> *.jpg
/S
creates and copys subfolders as well. You can also use /DCOPY:T
to make the directories have the same timestamp than the original ones or /L
to preview the actions of robocopy.
Small problem: The /E
switch handles subfolders as well, even if they are empty. /S
handles subfolders as well, but not, if they are empty. But it handles them, if they are not empty, but have no JPG inside -- so, subfolders without JPGs will result in empty folders in the target folder.
Robocopy has loads of parameters, so check out robocopy /?
.
Hope this helps! :-)
1Found here: How to traverse folder tree/subtrees in a windows batch file?
2Found here: http://www.dostips.com/DtTutoFunctions.php