# Dos Batch file - Search for JPG files in certain directory



## rhatton (Jun 23, 2011)

Hi there, I want to make a batch file that searches a specific directory for *.JPG files. These files will be contained in subfolders. 
e.g search in 
C:\Documents and Settings\user\Desktop\JPG files\ 
The files will be located in these subfolders
C:\Documents and Settings\user\Desktop\JPG files\ Folder 1
C:\Documents and Settings\user\Desktop\JPG files\ Folder 2
C:\Documents and Settings\user\Desktop\JPG files\ Folder 3
C:\Documents and Settings\user\Desktop\JPG files\ Folder 4
I want to select all these files and then MOVE them to a different directory and then delete all the subfolders these files were contained in.
e.g Search found :
C:\Documents and Settings\user\Desktop\JPG files\ Folder 1/file1.JPG
C:\Documents and Settings\user\Desktop\JPG files\ Folder 2/file2.JPG
C:\Documents and Settings\user\Desktop\JPG files\ Folder 3/file3.JPG
C:\Documents and Settings\user\Desktop\JPG files\ Folder 4/file4.JPG
Move file1.jpg, file2.jpg, file3.jpg and file4.jpg to -> C:\Documents and Settings\user\Desktop\JPG files\ 
then delete these directories
C:\Documents and Settings\user\Desktop\JPG files\ Folder 1
C:\Documents and Settings\user\Desktop\JPG files\ Folder 2
C:\Documents and Settings\user\Desktop\JPG files\ Folder 3
C:\Documents and Settings\user\Desktop\JPG files\ Folder 4

I have limited knowledge of batch files and i wouldnt even know how to approach this request! I would be really grateful if somebody could suggest something that would do this.

many thanks in advance!


----------



## mandraker (Aug 9, 2009)

I'm no expert on batch files, but the one you want seems very complicated. Would it not be so much easier to simply use Explorer to view all those folders, move the jpgs to your new folder, and then delete the empty folders?


----------



## scrfix (May 3, 2009)

rhatton,

What you want is not so far away. You would have to make a FOR loop that used the dir command to list specific files


```
@echo off
for /f "tokens=*" %%G in ('dir /a-d-h-s-l /oe /s /b "*.jpg" "%CD%" ^| FIND /I ".jpg"') do MOVE /Y "%%G" "%CD%"
:: Add RD Statements here
:: Add RD Statements here
:: Add RD Statements here
```
Use /-Y if you want it to prompt you to overwrite files with the same name. Right now it is suppressed with the /Y

Since I don't know the names of your directories I cannot put those in so simply add the following on the next lines below the FOR loop.
RD FOLDER1 /S /Q
RD FOLDER2 /S /Q

etc etc etc to the batch file to remove all of those directories. If FOLDER1 has 10 subfolders, all of those will be removed as long as they are not in use at the time of running the batch.

This will not copy .png, .gif or any other format other than .jpg files.

Please note: You will see "The system cannot find the file specified." in the output. If set echo to on instead of off, you will see that it is attempting to move the same file it has already moved. I am not sure why this is however that is why the error is there. Those errors can be ignored.

Run this batch in C:\Documents and Settings\user\Desktop\JPG files

Don't forget to mark this as solved.


----------



## Squashman (Apr 4, 2003)

scrfix said:


> Since I don't know the names of your directories I cannot put those in so simply add the following on the next lines below the FOR loop.
> RD FOLDER1 /S /Q
> RD FOLDER2 /S /Q


Well you could if you coded it differently.


----------



## scrfix (May 3, 2009)

I figured his most important thing was to move the .jpg which wasn't too bad. It did take a lot more testing than what I figured it would be but it works.


----------

