2

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
Magoo
  • 77,302
  • 8
  • 62
  • 84
Freedox
  • 123
  • 2
  • 4
  • 11
  • Did you write any code so far you could share? – Fred Nov 17 '14 at 15:43
  • 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 Nov 17 '14 at 15:53

4 Answers4

3

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

Community
  • 1
  • 1
Kurtibert
  • 638
  • 1
  • 5
  • 16
  • 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 – Freedox Nov 18 '14 at 10:40
  • Couldn't you just use `net use Y: \\intranet\PP Complete Images\ `? Then you can access the share via the drive letter `Y:`. – Kurtibert Nov 18 '14 at 10:47
  • 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 Nov 18 '14 at 11:13
  • but can I specify that destination ish shared foler + I need only *.jpg files? – Freedox Nov 18 '14 at 16:12
  • 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. – Kurtibert Nov 18 '14 at 20:34
2

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.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • 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 Nov 18 '14 at 12:12
  • 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 Nov 18 '14 at 12:16
0

I do believe the solution to your problem would be Robocopy. Robocopy is just plain awesome!

Here is the syntax of robocopy-

robocopy [Source] [Destination] [File] [...] [options]

Source Specifies the source folder. Where you want to take the files from.

Destination Destination directory/folder.

File Here we are! This is what will help you. Here you can specify an extension you want to move. So in your case, your code would look somewhat like this.

robocopy *.jpg c:\destinationdir /S /MAX:1048576

*To execute this .bat every morning go to a program called task scheduler, dont worry, its built into windows. http://windows.microsoft.com/en-US/windows/schedule-task#1TC=windows-7

*Then Click on Create basic task, and set your task to whenever you like!

Bijan
  • 7,737
  • 18
  • 89
  • 149
Pringles
  • 18
  • 7
0

Thanks guys for your help!!!

I got it solved and there is a code below if someone would ever need something similar:

    pushd Z:\intranet\PP Complete Images\
   for /r %%a in (*.jpg) do (
      XCOPY /Y "%%a" "C:\Users\username\Desktop\Master Image Folder"
   )
popd
Freedox
  • 123
  • 2
  • 4
  • 11