Windows batch extract zip files, into folders based on zip name and combining similar zips












1















I have been searching for a way to bulk extract zip files in a directory and extract them into individual folders based on their zip file name.



I found: Extract all Zip's in a directory (incl. subfolders) with a .bat file or dos command



and: https://stackoverflow.com/questions/17077964/windows-batch-script-to-unzip-files-in-a-directory



which do what I wanted. However I realised that I have some zip files that need to be extracted into the same directory. For example:



base dir
|
|
> file.zip
|
> another file.zip
|
> yaf_disk1.zip
|
> yaf_disk2.zip
|
> yaf2_disk1.zip
|
> yaf2_disk2.zip


With the above I would like yaf_disk1 and yaf_disk2 to be extracted into a directory yaf and yaf2_diskX into the directory yaf2.



I have a working script:



for /R "." %%I in ("*.zip") do (
"%ProgramFiles%7-Zip7z.exe" x -y -o"%%~dpnI" "%%~fI"
)


which I got from the above links which I mostly (now!) understand. I don't know if it is possible to get a batch file to look at the file, see the diskX at the end and extract all files with same name and diskX into the first directory it created from those files?



I am working with Windows 10 if that is of any importance.










share|improve this question





























    1















    I have been searching for a way to bulk extract zip files in a directory and extract them into individual folders based on their zip file name.



    I found: Extract all Zip's in a directory (incl. subfolders) with a .bat file or dos command



    and: https://stackoverflow.com/questions/17077964/windows-batch-script-to-unzip-files-in-a-directory



    which do what I wanted. However I realised that I have some zip files that need to be extracted into the same directory. For example:



    base dir
    |
    |
    > file.zip
    |
    > another file.zip
    |
    > yaf_disk1.zip
    |
    > yaf_disk2.zip
    |
    > yaf2_disk1.zip
    |
    > yaf2_disk2.zip


    With the above I would like yaf_disk1 and yaf_disk2 to be extracted into a directory yaf and yaf2_diskX into the directory yaf2.



    I have a working script:



    for /R "." %%I in ("*.zip") do (
    "%ProgramFiles%7-Zip7z.exe" x -y -o"%%~dpnI" "%%~fI"
    )


    which I got from the above links which I mostly (now!) understand. I don't know if it is possible to get a batch file to look at the file, see the diskX at the end and extract all files with same name and diskX into the first directory it created from those files?



    I am working with Windows 10 if that is of any importance.










    share|improve this question



























      1












      1








      1








      I have been searching for a way to bulk extract zip files in a directory and extract them into individual folders based on their zip file name.



      I found: Extract all Zip's in a directory (incl. subfolders) with a .bat file or dos command



      and: https://stackoverflow.com/questions/17077964/windows-batch-script-to-unzip-files-in-a-directory



      which do what I wanted. However I realised that I have some zip files that need to be extracted into the same directory. For example:



      base dir
      |
      |
      > file.zip
      |
      > another file.zip
      |
      > yaf_disk1.zip
      |
      > yaf_disk2.zip
      |
      > yaf2_disk1.zip
      |
      > yaf2_disk2.zip


      With the above I would like yaf_disk1 and yaf_disk2 to be extracted into a directory yaf and yaf2_diskX into the directory yaf2.



      I have a working script:



      for /R "." %%I in ("*.zip") do (
      "%ProgramFiles%7-Zip7z.exe" x -y -o"%%~dpnI" "%%~fI"
      )


      which I got from the above links which I mostly (now!) understand. I don't know if it is possible to get a batch file to look at the file, see the diskX at the end and extract all files with same name and diskX into the first directory it created from those files?



      I am working with Windows 10 if that is of any importance.










      share|improve this question
















      I have been searching for a way to bulk extract zip files in a directory and extract them into individual folders based on their zip file name.



      I found: Extract all Zip's in a directory (incl. subfolders) with a .bat file or dos command



      and: https://stackoverflow.com/questions/17077964/windows-batch-script-to-unzip-files-in-a-directory



      which do what I wanted. However I realised that I have some zip files that need to be extracted into the same directory. For example:



      base dir
      |
      |
      > file.zip
      |
      > another file.zip
      |
      > yaf_disk1.zip
      |
      > yaf_disk2.zip
      |
      > yaf2_disk1.zip
      |
      > yaf2_disk2.zip


      With the above I would like yaf_disk1 and yaf_disk2 to be extracted into a directory yaf and yaf2_diskX into the directory yaf2.



      I have a working script:



      for /R "." %%I in ("*.zip") do (
      "%ProgramFiles%7-Zip7z.exe" x -y -o"%%~dpnI" "%%~fI"
      )


      which I got from the above links which I mostly (now!) understand. I don't know if it is possible to get a batch file to look at the file, see the diskX at the end and extract all files with same name and diskX into the first directory it created from those files?



      I am working with Windows 10 if that is of any importance.







      windows command-line batch zip 7-zip






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 23 '17 at 12:41









      Community

      1




      1










      asked Jul 24 '16 at 8:00









      smartroadsmartroad

      613




      613






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Don't worry, created a Python script that done it for me. If anyone comes across this and needs something similar here is my code:



          import glob, os, zipfile

          zipfiles = glob.glob("*.zip") ## find all files with zip extension in current directory

          count = 0 ## current file

          while (count < len(zipfiles)):
          print ("Extracting file: "+zipfiles[count])

          zipRef = zipfile.ZipFile(zipfiles[count]) ## make ready for extraction

          directoryToMake = zipfiles[count] ## iniital name for extraction directory

          isMultiDisk = zipfiles[count].find('Disk') ## search for 'Disk' in the directory name and store location

          if (isMultiDisk > 0): ## has found 'Disk' in the name
          print("Multi Disk")
          if (len(directoryToMake[isMultiDisk+4:-4]) < 2): ## checks to see if the disk number is more than 2 characters long
          ## not actually checking the number just the number of characters
          ## to account for some names being DiskA rather than Disk1
          directoryToMake = directoryToMake[:-10] ## remove everything after the Disk to make the clean directory name
          else:
          directoryToMake = directoryToMake[:-11] ## remove extra char for disk numbers of 2 digits
          else: ## disk not found in name
          print("Single Disk")
          directoryToMake = directoryToMake[:-4] ## just remove the .zip at the end

          if not os.path.exists(directoryToMake): ## check if directory exists
          print("Creating Directory: " + directoryToMake)
          os.makedirs(directoryToMake) ## if not make it

          print("Extracting files into directory: " + directoryToMake+ "n")
          zipRef.extractall(directoryToMake) ## extract into directory

          count += 1





          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "3"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1104504%2fwindows-batch-extract-zip-files-into-folders-based-on-zip-name-and-combining-si%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Don't worry, created a Python script that done it for me. If anyone comes across this and needs something similar here is my code:



            import glob, os, zipfile

            zipfiles = glob.glob("*.zip") ## find all files with zip extension in current directory

            count = 0 ## current file

            while (count < len(zipfiles)):
            print ("Extracting file: "+zipfiles[count])

            zipRef = zipfile.ZipFile(zipfiles[count]) ## make ready for extraction

            directoryToMake = zipfiles[count] ## iniital name for extraction directory

            isMultiDisk = zipfiles[count].find('Disk') ## search for 'Disk' in the directory name and store location

            if (isMultiDisk > 0): ## has found 'Disk' in the name
            print("Multi Disk")
            if (len(directoryToMake[isMultiDisk+4:-4]) < 2): ## checks to see if the disk number is more than 2 characters long
            ## not actually checking the number just the number of characters
            ## to account for some names being DiskA rather than Disk1
            directoryToMake = directoryToMake[:-10] ## remove everything after the Disk to make the clean directory name
            else:
            directoryToMake = directoryToMake[:-11] ## remove extra char for disk numbers of 2 digits
            else: ## disk not found in name
            print("Single Disk")
            directoryToMake = directoryToMake[:-4] ## just remove the .zip at the end

            if not os.path.exists(directoryToMake): ## check if directory exists
            print("Creating Directory: " + directoryToMake)
            os.makedirs(directoryToMake) ## if not make it

            print("Extracting files into directory: " + directoryToMake+ "n")
            zipRef.extractall(directoryToMake) ## extract into directory

            count += 1





            share|improve this answer




























              0














              Don't worry, created a Python script that done it for me. If anyone comes across this and needs something similar here is my code:



              import glob, os, zipfile

              zipfiles = glob.glob("*.zip") ## find all files with zip extension in current directory

              count = 0 ## current file

              while (count < len(zipfiles)):
              print ("Extracting file: "+zipfiles[count])

              zipRef = zipfile.ZipFile(zipfiles[count]) ## make ready for extraction

              directoryToMake = zipfiles[count] ## iniital name for extraction directory

              isMultiDisk = zipfiles[count].find('Disk') ## search for 'Disk' in the directory name and store location

              if (isMultiDisk > 0): ## has found 'Disk' in the name
              print("Multi Disk")
              if (len(directoryToMake[isMultiDisk+4:-4]) < 2): ## checks to see if the disk number is more than 2 characters long
              ## not actually checking the number just the number of characters
              ## to account for some names being DiskA rather than Disk1
              directoryToMake = directoryToMake[:-10] ## remove everything after the Disk to make the clean directory name
              else:
              directoryToMake = directoryToMake[:-11] ## remove extra char for disk numbers of 2 digits
              else: ## disk not found in name
              print("Single Disk")
              directoryToMake = directoryToMake[:-4] ## just remove the .zip at the end

              if not os.path.exists(directoryToMake): ## check if directory exists
              print("Creating Directory: " + directoryToMake)
              os.makedirs(directoryToMake) ## if not make it

              print("Extracting files into directory: " + directoryToMake+ "n")
              zipRef.extractall(directoryToMake) ## extract into directory

              count += 1





              share|improve this answer


























                0












                0








                0







                Don't worry, created a Python script that done it for me. If anyone comes across this and needs something similar here is my code:



                import glob, os, zipfile

                zipfiles = glob.glob("*.zip") ## find all files with zip extension in current directory

                count = 0 ## current file

                while (count < len(zipfiles)):
                print ("Extracting file: "+zipfiles[count])

                zipRef = zipfile.ZipFile(zipfiles[count]) ## make ready for extraction

                directoryToMake = zipfiles[count] ## iniital name for extraction directory

                isMultiDisk = zipfiles[count].find('Disk') ## search for 'Disk' in the directory name and store location

                if (isMultiDisk > 0): ## has found 'Disk' in the name
                print("Multi Disk")
                if (len(directoryToMake[isMultiDisk+4:-4]) < 2): ## checks to see if the disk number is more than 2 characters long
                ## not actually checking the number just the number of characters
                ## to account for some names being DiskA rather than Disk1
                directoryToMake = directoryToMake[:-10] ## remove everything after the Disk to make the clean directory name
                else:
                directoryToMake = directoryToMake[:-11] ## remove extra char for disk numbers of 2 digits
                else: ## disk not found in name
                print("Single Disk")
                directoryToMake = directoryToMake[:-4] ## just remove the .zip at the end

                if not os.path.exists(directoryToMake): ## check if directory exists
                print("Creating Directory: " + directoryToMake)
                os.makedirs(directoryToMake) ## if not make it

                print("Extracting files into directory: " + directoryToMake+ "n")
                zipRef.extractall(directoryToMake) ## extract into directory

                count += 1





                share|improve this answer













                Don't worry, created a Python script that done it for me. If anyone comes across this and needs something similar here is my code:



                import glob, os, zipfile

                zipfiles = glob.glob("*.zip") ## find all files with zip extension in current directory

                count = 0 ## current file

                while (count < len(zipfiles)):
                print ("Extracting file: "+zipfiles[count])

                zipRef = zipfile.ZipFile(zipfiles[count]) ## make ready for extraction

                directoryToMake = zipfiles[count] ## iniital name for extraction directory

                isMultiDisk = zipfiles[count].find('Disk') ## search for 'Disk' in the directory name and store location

                if (isMultiDisk > 0): ## has found 'Disk' in the name
                print("Multi Disk")
                if (len(directoryToMake[isMultiDisk+4:-4]) < 2): ## checks to see if the disk number is more than 2 characters long
                ## not actually checking the number just the number of characters
                ## to account for some names being DiskA rather than Disk1
                directoryToMake = directoryToMake[:-10] ## remove everything after the Disk to make the clean directory name
                else:
                directoryToMake = directoryToMake[:-11] ## remove extra char for disk numbers of 2 digits
                else: ## disk not found in name
                print("Single Disk")
                directoryToMake = directoryToMake[:-4] ## just remove the .zip at the end

                if not os.path.exists(directoryToMake): ## check if directory exists
                print("Creating Directory: " + directoryToMake)
                os.makedirs(directoryToMake) ## if not make it

                print("Extracting files into directory: " + directoryToMake+ "n")
                zipRef.extractall(directoryToMake) ## extract into directory

                count += 1






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jul 24 '16 at 12:36









                smartroadsmartroad

                613




                613






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Super User!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1104504%2fwindows-batch-extract-zip-files-into-folders-based-on-zip-name-and-combining-si%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                    Alcedinidae

                    Can an atomic nucleus contain both particles and antiparticles? [duplicate]