| 1. |
Solve : I want to create a batch script which can search for .jpg and .jpeg? |
|
Answer» I want to create a batch script which can search for .jpg and .jpeg search for .jpg and .jpeg Quote once found they must get copied. Well, assuming you read the question, your output "proves" that your code doesn't work Here is what I got. 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 ) ) This script searches the physical drive but does not search into the folders and subfolders, so can anyone modify the script and give me a script which will fully search the physical drive including folders and subfolders.I want to help you, but in my own way. For me to use your script would be a chore. My approach to this, and other jobs, is to consider the objective. First Step: Objective: Once the objective is know, it is time to create an abs tact idea or concept. Then create a method that is suitable. The objective has to be reasonable. The provided a script does not indicate a clearly stated objective. Second Step: Method. Batch MAY be a suitable method. A short, concise command is admirable, butcan be difficult to adapt. Breaking a hob into tasks can help. Third Step: Use restraint. The Operating system has some practical limits on what it can do at one time.In Batch there is a PROBLEM of DELAYED execution and extentions enbabled. Also placing an unlimited number of files of unknown size into one directory may crash the system. The FOR loop contains not only technical syntax problems but also weak logic. Do you understand the , TAKEN literally, your idea can make the Operating system unstable. It may even produce a fatal error. And there's more... but I will let others say.OKay then can yoiu help me with a single drive. Lets take with D Drive, there are few pictures that I need to get copied, will you help me with this please.before running this snippet, make sureto change the destfolder. Code: [Select]SETLOCAL EnableDelayedExpansion set destfolder=D:\copiedjpegs for /F %%a in ('mountvol ^| find ":\"') do ( for /f "tokens=*" %%P in ('dir %%a\*.jp*g /s /b') do copy %%P !destfolder! ) only problem I can think of for the above is that *.jp*g can match jpog and jpag and so forth; but these extensions aren't common file types so likely won't be encountered. Also, since it will search ALL drives, the destination folder will inevitably be searched; I believe it will error at that point but the loop should simply report the error (file cannot be copied onto itself) and continue. I'm also uncertain wether I need the EXCLAMATION marks or delayed expansion, since destfolder remains unchanged during the loop. that din't worked. Quote from: akki15623 on December 16, 2009, 12:43:01 PM that din't worked.explain. EDIT: it was a simple typo that I changed in my copy but not in the copy I posted. Code: [Select] SETLOCAL EnableDelayedExpansion set destfolder=D:\copiedjpegs for /F %%a in ('mountvol ^| find ":\"') do ( for /f "tokens=*" %%P in ('dir %%a*.jp*g /s /b') do copy "%%P" !destfolder! ) there was a slash between the %%a and the *.jp*g, which is not necessary since mountvol includes the slash. EDIT: Oh, and I forgot quotes. Quote from: BC_Programmer on December 16, 2009, 12:32:57 PM only problem I can think of for the above is that *.jp*g can match jpog and jpag and so forth Perhaps... Code: [Select]('dir /s /b %%a*.jp*g ^| find ".jpg ^| find ".jpeg" /s /b') Or explicitly test for the wanted extensions... Code: [Select]set destfolder=D:\copiedjpegs for /F %%a in ('mountvol ^| find ":\"') do ( for /f "tokens=*" %%P in ('dir %%a*.jp*g /s /b') do ( if /i "%%~xP"==".jpg" copy "%%P" !destfolder! if /i "%%~xP"==".jpeg" copy "%%P" !destfolder! ) ) Quote I'm also uncertain wether I need the exclamation marks or delayed expansion, since destfolder remains unchanged during the loop. You don't. Quote from: BC_Programmer on December 16, 2009, 12:43:17 PM explain. Worked perfectly...... Thanks, thank you very much buddy...... Quote from: akki15623 on December 16, 2009, 01:24:03 AM I want to create a batch script which can search for .jpg and .jpeg C:\batch>type bc1215.bat Code: [Select]SETLOCAL EnableDelayedExpansion set destfolder=E:\copiedjpegs E: dir /s /b *.jpg > jpgfiles.txt for /f "tokens=*" %%P in (jpgfiles.txt ) do copy %%P !destfolder! ) dir E:\copiedjpegs\ | more OUTPUT: C:\batch> bc1215.bat Directory of E:\copiedjpegs 12/16/2009 02:21 PM . 12/16/2009 02:21 PM .. 05/14/2005 03:44 PM 1,390,722 002.jpg 12/26/2002 11:26 AM 63,683 03.JPG 05/03/2006 01:05 PM 2,199,973 050206.jpg 02/03/2003 01:32 AM 20,443 1.jpg 02/03/2003 01:31 AM 11,601 10.jpg 02/03/2003 01:23 AM 11,178 11.jpg 02/03/2003 01:21 AM 16,492 12.jpg 11/02/2002 06:05 PM 84,884 14.jpg 12/29/2004 10:01 PM 10,039 1map.jpg 02/19/2006 01:02 PM 2,318,092 2-18-06.jpg 02/03/2003 01:10 AM 15,026 2.jpg 12/28/2004 10:57 AM 30,112 20_months.jpg 12/29/2004 10:01 PM 8,086 2map.jpg 02/03/2003 01:18 AM 199,266 3.jpg 12/28/2002 04:14 AM 30,008 4.jpg 11/12/1998 08:48 PM 62,325 457015.jpg 11/12/1998 08:50 PM 71,110 457030.jpg 11/12/1998 09:46 PM 104,355 487071.jpg 11/12/1998 09:46 PM 70,428 487072.jpg 02/03/2003 01:18 AM 5,437 5.jpg 11/12/1998 10:07 PM 57,326 577092.jpg 11/12/1998 07:52 PM 86,958 594008.jpg 12/29/2004 10:01 PM 10,039 5map.jpg 02/03/2003 01:16 AM 8,193 6.jpg 11/13/1998 02:45 AM 51,505 643047.jpg 11/13/1998 02:46 AM 55,217 643057.jpg 11/11/1998 10:25 PM 101,750 677023.jpg 11/12/1998 02:11 AM 82,308 682008.jpg 11/12/1998 02:25 AM 92,993 682097.jpg 04/28/2002 07:12 PM 25,813 7.jpg 03/29/2005 11:41 PM 104,563 70.jpg 11/12/1998 01:30 AM 31,173 719024.jpg 11/12/1998 01:32 AM 55,994 719038.jpg 11/12/1998 01:40 AM 63,905 719090.jpg 11/12/1998 01:41 AM 44,786 719098.jpg 11/12/1998 10:30 PM 75,614 762000.jpg 11/12/1998 10:39 PM 69,560 762055.jpg C:\batch> Quote from: BC_Programmer on December 16, 2009, 12:43:17 PM explain. Can you help me with one more thing. I will send the above script to my girlfriend PC, is there a way that you can modify this so that it can send all the pics to my e-mail???um... no I don't think so. you could zip them via the command-line but the attachment will be huge... and I don't know how to do SMTP/e-mail from batch. |
|