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