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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
akki15623
Posts: 2
Joined: 15 Dec 2009 20:22
Contact:

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

#1 Post by akki15623 » 15 Dec 2009 20:27

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.

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 16 Dec 2009 11:18

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%"

akki15623
Posts: 2
Joined: 15 Dec 2009 20:22
Contact:

#3 Post by akki15623 » 16 Dec 2009 12:52

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
)
)

thr333
Posts: 16
Joined: 24 Aug 2009 10:24

#4 Post by thr333 » 17 Dec 2009 08:36

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?

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#5 Post by avery_larry » 18 Dec 2009 15:38

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.

thr333
Posts: 16
Joined: 24 Aug 2009 10:24

#6 Post by thr333 » 19 Dec 2009 18:59

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.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

#7 Post by !k » 20 Dec 2009 04:59

thr333
Start C:\WINDOWS\Help\ntcmds.chm and read more detailed help on the DIR command with a few examples.

thr333
Posts: 16
Joined: 24 Aug 2009 10:24

#8 Post by thr333 » 23 Dec 2009 02:55

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

Hawk
Posts: 4
Joined: 24 Dec 2009 23:34
Location: India

A small Clarification...

#9 Post by Hawk » 24 Dec 2009 23:39

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.... :)

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#10 Post by avery_larry » 26 Dec 2009 20:39

%%~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.

Hawk
Posts: 4
Joined: 24 Dec 2009 23:34
Location: India

Wooww...

#11 Post by Hawk » 27 Dec 2009 06:09

Thats an interesting point...

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

Regards - Pradeep

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

Post Reply