Command Promp to copy ONLY images from FOLDERS and SUBFOLDERS

15,103

Solution 1

Your existing code:

the cd c: is incorrect. To switch the current drive to c: use

c:

The cd \ is redundant. Your remaining code specifies the directories, so the current directory is irrelevant.

Your first copy command has three problems. Master Image Folder*.jpg means all filenames beginning Master Image Folder and ending .jpg. You probably meant Master Image Folder\*.jpg meaning all files ending .jpg in ...\Master Image Folder\

C:\Users\username\Deskto... is probably an error. It is a literal path, so the actual directory would be C:\Users\username\Deskto... You would probably need C:\Users\%username%\Deskto... to substitute-in the current username.

And then the job would stop on a filename-match, so either you'd be pressing A to overwrite all or you'd be pressing y or n for each name-match.

Your final copy command has no specified destination directory.

You can edit-in your actual code by using the edit button under the original text window, cutting-and-pasting your actual code - censoring if necessary, selecting the resultant code block and pressing the {} button above the edit box which indents each line with the effect of formatting and hilighting the code.

The simplest solution is probably to use

xcopy /d /y /s "\\intranet\PP Complete Images\Master Image Folder\*.jpg" "C:\Users\%username%\Desktop\Master Image Folder\" 

which will copy updated files (/d) with automatic overwrite (/y) and scanning subdirectories (/s) from-name/mask to-directory.

This would create an identical directory-hierarchy to the original subtree under the destop's Master Image Folder directory.

You could extend this to

for %%a in (
 "\\intranet\PP Complete Images\Master Image Folder"
 "\\intranet\wherever\somewhere"
) do xcopy /d /y /s "%~a\*.jpg" "C:\Users\%username%\Desktop\Master Image Folder\"

to perform the same action on multiple directory-subtrees; but you need to ensure that the destination directory is not within any subtree selected for inclusion in the list within the parentheses.

I'd advise against "flattening" the output because if you do that, the latest whatever.jpg from each of the subtrees will end up in your destination directory, without notification that there are many possibly different whatever.jpg versions.

Solution 2

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.jpgs 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

Share:
15,103
Freedox
Author by

Freedox

Updated on June 05, 2022

Comments

  • Freedox
    Freedox over 1 year

    I'm wondering if it is possible to set up a batch command to perform this action.

    Once .bat file is executed, ALL images from folders and sub-folders would be copied to my location on the desktop.

    Example: Original folder is located: \intranet\file_location\PP Complete Images (in this folder will be loads of other folders and in those folders there will be .jpg images)

    Destination file would be based on the desktop.

    So I need to extract .jpg images from all folders and sub-folders.

    If image already exists in original folder, skip the image or overwrite as script will be executed every morning.

    Or should I look for a software to do this for me?

    Existing code:

    cd c: 
    cd\ 
    copy "\\intranet\PP Complete Images\Master Image Folder*.jpg" "C:\Users\username\Desktop\Master Image Folder" 
    copy "\\intranet\PP Complete Images*.jpg" 
    exit
    
    • Fred
      Fred almost 9 years
      Did you write any code so far you could share?
    • Freedox
      Freedox almost 9 years
      Yes, but it only copies if .jpg files are in one folder: cd c: cd\ copy "\\intranet\PP Complete Images\Master Image Folder*.jpg" "C:\Users\username\Desktop\Master Image Folder" copy "\\intranet\PP Complete Images*.jpg" exit
  • Freedox
    Freedox almost 9 years
    I'm looking for a Flattened solution and that script works just right with one BUT... I cant' run batch file on shared intranet so I need to have a source link to one folder where images would be pulled from
  • Kurtibert
    Kurtibert almost 9 years
    Couldn't you just use net use Y: \\intranet\PP Complete Images\ ? Then you can access the share via the drive letter Y:.
  • Freedox
    Freedox almost 9 years
    This is a shred drive withing a company This is a link that I use to access all the folders in destination: \\btbnas\PP Complete Images
  • Freedox
    Freedox almost 9 years
    Thing is that all images in folders and sub-folders are unique so I'm not worrying about duplicating them... The issue is that I can't run batch files on shared folder so it has to be my local machine...
  • Freedox
    Freedox almost 9 years
    I have combined code to this: @ECHO OFF for /r %%f in ("\\intranet\PP Complete Images") do call:copyFile %%f GOTO END :copyFile copy /V %~1 C:\Users\username\Desktop\test GOTO:EOF :END but I need to pull only .jpg files and it actually are pulling everything and only pulls 24 items out of 40k+ images
  • Freedox
    Freedox almost 9 years
    but can I specify that destination ish shared foler + I need only *.jpg files?
  • Kurtibert
    Kurtibert almost 9 years
    The for command finds all element from the current working directory. So, you can use net use Y: \\intranet\PP Complete Images\ to map Y: to your share. Afterwards, the command Y: changes the working directory to your share.