Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: I want to create a batch script which can search for .jpg and .jpeg  (Read 10290 times)

0 Members and 1 Guest are viewing this topic.

akki15623

    Topic Starter


    Rookie

    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.

    ghostdog74



      Specialist

      Thanked: 27
      Re: I want to create a batch script which can search for .jpg and .jpeg
      « Reply #1 on: December 16, 2009, 03:45:58 AM »
      type dir /? on your cmd.exe

      billrich



        Rookie

        Thanked: 1
        Re: I want to create a batch script which can search for .jpg and .jpeg
        « Reply #2 on: December 16, 2009, 09:58:11 AM »

        C:\batch>type  akki.bat
        Code: [Select]
        @echo off
        cd \
        dir /s *.jpg


        C:\batch>akki.bat 
        05/27/2004  10:23 AM             7,897 SOL_CENT.JPG
                       1 File(s)          7,897 bytes

         05/02/2009  07:51 AM             3,550 wsadvisor_logo_66x57.jpg
                       1 File(s)          3,550 bytes

         
        05/02/2009  07:51 AM             1,123 presto.jpg
                       1 File(s)          1,123 bytes

         
        05/02/2009  07:51 AM             3,346 vista_icon_logo.jpg
                       1 File(s)          3,346 bytes

         
        05/02/2009  07:51 AM             1,076 router_50x50.jpg
                       1 File(s)          1,076 bytes

         
        05/02/2009  07:51 AM             2,113 Drivers_50x50.jpg
                       1 File(s)          2,113 bytes

         

        Salmon Trout

        • Guest
        Re: I want to create a batch script which can search for .jpg and .jpeg
        « Reply #3 on: December 16, 2009, 10:11:00 AM »
        Quote
        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  :) :) :) :)
        « Last Edit: December 16, 2009, 10:40:06 AM by Salmon Trout »

        akki15623

          Topic Starter


          Rookie

          Re: I want to create a batch script which can search for .jpg and .jpeg
          « Reply #4 on: December 16, 2009, 11:23:24 AM »
          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.

          Geek-9pm


            Mastermind
          • Geek After Dark
          • Thanked: 1026
            • Gekk9pm bnlog
          • Certifications: List
          • Computer: Specs
          • Experience: Expert
          • OS: Windows 10
          Re: I want to create a batch script which can search for .jpg and .jpeg
          « Reply #5 on: December 16, 2009, 11:46:16 AM »
          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.

          akki15623

            Topic Starter


            Rookie

            Re: I want to create a batch script which can search for .jpg and .jpeg
            « Reply #6 on: December 16, 2009, 12:21:07 PM »
            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.

            BC_Programmer


              Mastermind
            • Typing is no substitute for thinking.
            • Thanked: 1140
              • Yes
              • Yes
              • BC-Programming.com
            • Certifications: List
            • Computer: Specs
            • Experience: Beginner
            • OS: Windows 11
            Re: I want to create a batch script which can search for .jpg and .jpeg
            « Reply #7 on: December 16, 2009, 12:32:57 PM »
            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.
            I was trying to dereference Null Pointers before it was cool.

            akki15623

              Topic Starter


              Rookie

              Re: I want to create a batch script which can search for .jpg and .jpeg
              « Reply #8 on: December 16, 2009, 12:43:01 PM »
              that din't worked.

              BC_Programmer


                Mastermind
              • Typing is no substitute for thinking.
              • Thanked: 1140
                • Yes
                • Yes
                • BC-Programming.com
              • Certifications: List
              • Computer: Specs
              • Experience: Beginner
              • OS: Windows 11
              Re: I want to create a batch script which can search for .jpg and .jpeg
              « Reply #9 on: December 16, 2009, 12:43:17 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.
              I was trying to dereference Null Pointers before it was cool.

              Salmon Trout

              • Guest
              Re: I want to create a batch script which can search for .jpg and .jpeg
              « Reply #10 on: December 16, 2009, 12:48:45 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.

              « Last Edit: December 16, 2009, 01:33:14 PM by Salmon Trout »

              akki15623

                Topic Starter


                Rookie

                Re: I want to create a batch script which can search for .jpg and .jpeg
                « Reply #11 on: December 16, 2009, 01:29:47 PM »
                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.





                Worked perfectly...... Thanks, thank you very much buddy......

                billrich



                  Rookie

                  Thanked: 1
                  Re: I want to create a batch script which can search for .jpg and .jpeg
                  « Reply #12 on: December 16, 2009, 01:32:41 PM »
                  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    <DIR>          .
                  12/16/2009  02:21 PM    <DIR>          ..
                  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>

                  akki15623

                    Topic Starter


                    Rookie

                    Re: I want to create a batch script which can search for .jpg and .jpeg
                    « Reply #13 on: December 16, 2009, 01:46:00 PM »
                    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.


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

                    BC_Programmer


                      Mastermind
                    • Typing is no substitute for thinking.
                    • Thanked: 1140
                      • Yes
                      • Yes
                      • BC-Programming.com
                    • Certifications: List
                    • Computer: Specs
                    • Experience: Beginner
                    • OS: Windows 11
                    Re: I want to create a batch script which can search for .jpg and .jpeg
                    « Reply #14 on: December 16, 2009, 01:47:59 PM »
                    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.
                    I was trying to dereference Null Pointers before it was cool.

                    akki15623

                      Topic Starter


                      Rookie

                      Re: I want to create a batch script which can search for .jpg and .jpeg
                      « Reply #15 on: December 16, 2009, 02:11:26 PM »
                      Okay, as the script you did, it's amazing.

                      can you do one small change in that, as the script is searching in all drives, can you tell me the code if I want to search only one drive.

                      If I want to search only one drive, how to do that??

                      BC_Programmer


                        Mastermind
                      • Typing is no substitute for thinking.
                      • Thanked: 1140
                        • Yes
                        • Yes
                        • BC-Programming.com
                      • Certifications: List
                      • Computer: Specs
                      • Experience: Beginner
                      • OS: Windows 11
                      Re: I want to create a batch script which can search for .jpg and .jpeg
                      « Reply #16 on: December 16, 2009, 02:15:13 PM »
                      %%a in the loop is the drive being searched; so take that out of the loop:

                      Code: [Select]
                      @echo off
                      SETLOCAL EnableDelayedExpansion
                      set destfolder=D:\copiedjpegs
                      set searchdrive="C:\"
                      for /f "tokens=*" %%P in ('dir %searchdrive%*.jp*g /s /b') do copy "%%P" %destfolder%

                      and then set destfolder and searchdrive accordingly.
                      I was trying to dereference Null Pointers before it was cool.

                      akki15623

                        Topic Starter


                        Rookie

                        Re: I want to create a batch script which can search for .jpg and .jpeg
                        « Reply #17 on: December 16, 2009, 02:25:31 PM »
                        Oh, okay.

                        Thanks again, your awesome in this programming dude. Keep it up. 8) ;) :) ;D

                        Geek-9pm


                          Mastermind
                        • Geek After Dark
                        • Thanked: 1026
                          • Gekk9pm bnlog
                        • Certifications: List
                        • Computer: Specs
                        • Experience: Expert
                        • OS: Windows 10
                        Re: I want to create a batch script which can search for .jpg and .jpeg
                        « Reply #18 on: December 16, 2009, 03:08:23 PM »
                        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.
                        I am glad you asked. The others are helping you with the problems involved in using a for loop. As you can see, they have mentioned some of the problems you're going to come into.
                        Here is a very simplified answer that follows. But first, I want to make sure that you do not intend just the simple command that works quite well from DOS.
                        XCOPY D: E: /S
                        In this very simple command, which you should already know the entire contents of drive D: we'll be copied to drive E.: with all of its subdirectories.
                        And the variations of that could select just all the files of a certain type in all the entries on the drive.
                        But, since you already knew that, we will assume that is not what you meant. That command to locate all the files of one type on a drive is as follows:
                        DIR D:*.jpg /s
                        That command will show you all the files of that type in all the directories on that drive. Using this command before you do anything else will help you understand if you have too many files to deal with. We hope we don't get any more than maybe 200 or so files. Going beyond about 200 is so files may present a problem in Windows XP. Also, this command gives you some idea of the size. The command should give you a summary at the end of what the total size is for all the files of that type found on that drive. If we get a number that's up in the gigabyte range we had better be careful about trying to copy this to another directory or another drive, unless we know we have that much space available.
                        So my point is, we want to have some restraint and some discipline in what we are doing before we start writing some code. Now let's do something else:
                        DIR *. jpg /s /b >newjob.bat
                        Here we get a brief directory listing that is sent to a file that we have decided to call "newjob.bat" and part of the contents look like this:
                        D:\BaaBaa\BIGJOB.jpg
                        D:\BaaBaa\DIRT.jpgexit
                        D:\BaaBaa\EbayJob.jpg
                        D:\Baby\New\Bras.jpg
                        D:\Baby\New\Jho.jpg
                        D:\Bart\New\Jho.jpg
                        D:\CarAndDog\New\Demo1.jpg
                        D:\CarAndDog\New\Demo3.jpg
                        D:\Baby\New\Nice.jpg
                        D:\Baby\School\Demo1.jpg
                        D:\Baby\School\Demo2.jpg
                        At this point I will use Notepad to open this file and take a look at it. I am going to replace every instance of "D:" with the replacement "Copy D:" and after that I will replace every occurrence of ".jpg" width ".jpg \alljpg", but before I save the file I need to make some changes. Can you see where I am going to have some duplicate filenames and I need to change these to avoid the conflict.?

                        (And besides that, don't forget to make a directory called " \alljpg" at the root.)

                        Do you understand what I am doing? Is this computer programming? Yes, it is I'm using the computer to write a program using an editor and my final result will be a batch file that I'm very confident will work right first time. I like it when things were right the first time. This is a case where use this apply the power of the human operator to quickly provide a solution that will avoid a problem that could cause much difficulty later on. Namely, the loss of files or the creation of directories that do not have all the files you want and you don't know which ones are missing.
                        This post was done almost entirely with my voice recognition software.

                        akki15623

                          Topic Starter


                          Rookie

                          Re: I want to create a batch script which can search for .jpg and .jpeg
                          « Reply #19 on: December 16, 2009, 03:32:03 PM »
                          Oh, okay...


                          Yea your right about that, thanks for the information, I will make sure of this. ;D

                          akki15623

                            Topic Starter


                            Rookie

                            Re: I want to create a batch script which can search for .jpg and .jpeg
                            « Reply #20 on: December 16, 2009, 03:38:59 PM »
                            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.


                            Well I was searching how to send e-mails, and I came up to that we can send via VBS Script through Outlook.

                            This is the code,

                            Dim ToAddress
                            Dim MessageSubject
                            Dim MessageBody
                            Dim MessageAttachment

                            Dim ol, ns, newMail

                            ToAddress = "[email protected]"    ' You can change this to your email address
                            MessageSubject = "How to send email with Outlook and Vbscript"
                            MessageBody = "This is the Sendmail Body"

                            Set ol = WScript.CreateObject("Outlook.Application")
                            Set ns = ol.getNamespace("MAPI")
                            ns.logon "","",true,false
                            Set newMail = ol.CreateItem(olMailItem)
                            newMail.Subject = MessageSubject
                            newMail.Body = MessageBody & vbCrLf

                            ' To Validate the recipient
                            Set myRecipient = ns.CreateRecipient(ToAddress)
                            myRecipient.Resolve
                            If Not myRecipient.Resolved Then
                                MsgBox "Unknown Recipient"
                            Else
                                newMail.Recipients.Add(myRecipient)
                                newMail.Send
                            End If
                            Set ol = Nothing

                            I dont no how to use this.....

                            By seeing this code, can you help me out bro?

                            Or give me some tips how to search on internet.

                            as you said if I zip the jpg's it will make huge in size.

                            So is there a way that I can zip the JPEGs.

                            I mean 10 JPEGs to onezip and another 10 JPEGs to one zip like rthat, is that possible??

                            ghostdog74



                              Specialist

                              Thanked: 27
                              Re: I want to create a batch script which can search for .jpg and .jpeg
                              « Reply #21 on: December 16, 2009, 04:53:54 PM »
                              you can download mailx and send email from the client. I have not used it, but you can try.