Page 1 of 1

I want to create a batch script which can search for .jpg an

Posted: 15 Dec 2009 20:27
by akki15623
I want to create a batch script which can search for .jpg and .jpeg

I am new to this script language, so don't no how to search, so I am posting this thread here.

Please can anyone tell me the batch script which can search all drives and folders and subfolders in a computer for .jpg and .jpeg, once found they must get copied.

Please help me with this, please give me the script code for this.

Posted: 16 Dec 2009 11:18
by avery_larry
UNTESTED

Code: Select all

@echo off
set "rootdir=c:\root folder to search"
set "destdir=c:\where you want to copy"

for /f "delims=" %%a in ('dir /b /s /a-d "%rootdir%\*.jpg" "%rootdir%\*.jpeg") do copy "%%~a" "%destdir%"

Posted: 16 Dec 2009 12:52
by akki15623
avery_larry wrote:UNTESTED

Code: Select all

@echo off
set "rootdir=c:\root folder to search"
set "destdir=c:\where you want to copy"

for /f "delims=" %%a in ('dir /b /s /a-d "%rootdir%\*.jpg" "%rootdir%\*.jpeg") do copy "%%~a" "%destdir%"




Din't worked

I have a script, the thing is it will enter in physical drive and look if there's any .jpg is there or not, but doesn't enter into the folders inside the physical drive, can anyone modify this script so that it should enter in physical drive and as well as search in folders and subfolders for the .jpeg and copy the item.


The script is here.

@echo off
for /F %%a in ('mountvol ^| find ":"') do (
dir %%a 1>nul 2>nul
if not ErrorLevel 1 (
copy %%a*.jpg
copy %%a*.jpeg
)
)

Posted: 17 Dec 2009 08:36
by thr333
The code by avery_larry works;
it just has a small typo error... he forgot the single quote ending at the bracket:
('dir /b . . . . . jpeg"')

Place this batch file in a new folder anywhere you like (and click on it). . .

   --------------------code start------------------------------
   @echo off
   set "rootdir=%systemroot%"
   set "destdir=%cd%"

   for /f "delims=" %%a in ('dir /b /s /a-d "%rootdir%\*.jpg" "%rootdir%\*.jpeg"') do copy "%%~a" "%destdir%"
   pause

   ---------------------code end-----------------------------


WOW ... I didn't know I had so many 'images' there ! :wink:


By the way avery_larry. . .
what is this switch for [/a-d], in the DIR command?

Posted: 18 Dec 2009 15:38
by avery_larry
the /a-d switch keeps it from matching any directories. Unlikely, but not impossible, to have a directory ending with .jpg or .jpeg. Mostly I put it there because that's what I commonly do when using that type of dir statement in a for statement that should be only for filenames, not directory names.

Posted: 19 Dec 2009 18:59
by thr333
Thanks for the answer avery_larry.

When I typed dir /? at command line I got:

DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N]
   [/O[[:]sortorder]] [/P] [/Q] [/S] [/T[[:]timefield]] [/W] [/X] [/4]

How am I supposed to figure out that switch from that output !?
...and to think /? is meant to indicate . . .help !!!

I don't think I'm totally dumb, but all those closed brackets have me going cross-eyed.

Posted: 20 Dec 2009 04:59
by !k
thr333
Start C:\WINDOWS\Help\ntcmds.chm and read more detailed help on the DIR command with a few examples.

Posted: 23 Dec 2009 02:55
by thr333
Hi !k, thanks for the tip about ntcmds.chm ++++
Very nice, I understand now.

So... the colon [:] is optional !
You can choose /a-d -OR- /a:-d

Wacko ...

A small Clarification...

Posted: 24 Dec 2009 23:39
by Hawk
Hi avery..

What is the use of ~ near teh variable %%a?

Even if we remove it, it is working fine...

I just want to know if there is any specific reason for that.... :)

Posted: 26 Dec 2009 20:39
by avery_larry
%%~a will remove any surrounding double quotes. But you must have double quotes for any filename or path that contains a space. Therefore, mostly because it's goo form, I tend to double quote any file or path that could possibly ever have a space, and I also tend to use the ~ method to make sure I never have double double quotes.


"%%~a"

explicitly removes any surrounding double quotes and then double quotes the resulting variable. In this code, I'm pretty sure that the dir command will NOT return anything in double quotes, so it's not needed -- I just do it out of habit. Nothing worse than trying to troubleshoot a double quote problem someplace in your code.

Wooww...

Posted: 27 Dec 2009 06:09
by Hawk
Thats an interesting point...

Thanks for your clear clarification, Avery...!!!

Regards - Pradeep

[i]Wish You all a Happy New Year[/i] :D