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

49

Solution 1

The Script:

for /F %%I IN ('dir /b /s *.zip *.rar') DO (
    "C:\Program Files\7-Zip\7z.exe" x -o"%%~dpI" "%%I"
)

Explanation:

for /F %%I IN ('dir /b /s *.zip *.rar') DO (

This performs a loop for each file returned by the command dir /b /s *.zip *.rar. The /s tells dir to recurse into subdirectories and /b prints in bare format.

The filename is stored in the %%I variable for use later. If you were typing this at the prompt, you would use %I instead.

"C:\Program Files\7-Zip\7z.exe" x -o"%%~dpI" "%%I"

This performs the extraction. The argument -o"%%~dpI" extracts the file into the same directory where the archive resides. Other options:

  • -o"%%~dpI" — Extracts into the directory where the archive resides.

  • -o"%%~dpnI" — Creates a new directory in the hierarchy named after the archive and extracts there (that is, AFolder\archive.zip extracts into AFolder\archive\).

  • -o"%%~nI" — Creates a new directory in the current directory named after the archive and extracts there (that is, AFolder\archive.zip extracts into .\archive\).

  • Omit the -o argument — Extracts into the current directory.

Example:

C:\Temp>tree /F

    Folder PATH listing
    Volume serial number is 08A4-22E0
    C:.
    │   batch.bat
    │
    ├───AFolder
    │       a.zip
    │
    ├───BFolder
    │       b.zip
    │
    └───CFolder
            c.zip



C:\Temp>batch.bat > nul


C:\Temp>tree /F

    Folder PATH listing
    Volume serial number is 08A4-22E0
    C:.
    │   batch.bat
    │
    ├───AFolder
    │       a.zip
    │       a.zip.txt
    │
    ├───BFolder
    │       b.zip
    │       b.zip.txt
    │
    └───CFolder
            c.zip
            c.zip.txt

Solution 2

This is an update of the accepted answer to support filenames with spaces ("DELIMS=") and skip overwrite (-aos). See links below and updated code. Thanks

Extract all Zip's in a directory (incl. subfolders) with a .bat file or dos command https://stackoverflow.com/questions/12487491/how-to-handle-space-of-filename-in-batch-for-loop http://7zip.bugaco.com/7zip/MANUAL/switches/overwrite.htm

for /F "DELIMS=" %%I IN ('dir /b /s *.zip *.rar') DO (
    "H:\Program Files\7-Zip\7z.exe" x -aos -o"%%~dpI" "%%I"
)

Solution 3

I just use sweep.exe from years ago. It runs the same command in the current directory and all subdirectories.

You may have to run sweep more than once if you are looking to extract archives within an archive.

You can use something like:

sweep 7za x -y *.zip

to open all .zip files in the current folder and all folders underneath.

7zip command line version is here: http://www.7-zip.org/download.html

Solution 4

I believe that you are looking for the forfiles command:

  • forfiles /s /m *.zip /c "7z x @file"

  • forfiles /s /m *.rar /c "7z x @file"

Share:
49

Related videos on Youtube

dats
Author by

dats

Updated on September 18, 2022

Comments

  • dats
    dats over 1 year

    I have this database schema as shown in the image below:

    enter image description here

    It was fine until I need to change a few things. So, some countries now have divisions within them. And each division may not have the same processes under them.

    For ex, Germany has 3 divisions (Div1, Div2, Div3). And I have some processes applicable to all Germany, some are only applicable to Div1.

    I am planning to create a new database, country_division, with the ff schema:

    division_country
    ----------------
    division_id
    countrycode
    division_name
    

    But then I would there would be a conflict with my process_country table, when I need to add an entry to add a process to a country with divisions.

    How do I redesign my database in such a way that it will be applicable to all countries?

    Edit: Based on your comments, this is what I created. Please let me know if this will work.

    enter image description here

    • Stivan
      Stivan about 8 years
      It seems to me you need to create either a new table for division or a new column for divisions in the country table. Use the division_id as foreign key and link process_country with divion_id rather than country_code. Where country code is no longer primary key. division_id will be your primary and foreign key. If you need additional explanation let me know I could explain more in a solution
    • dats
      dats about 8 years
      @Stivan I have edited my post, is that what you mean?
    • Stivan
      Stivan about 8 years
      I misread your requirements. The second diagram on your post will work. I thought each process will only have one division linked to it. But if each process may have many divisions you need 4 tables like you have done. In the Process_Country_Division table you done need FK to Country code. It is already linked in the Division table.
  • Email
    Email over 12 years
    thanks for your tip. it does not work like intented. sorry i am not that firm in dos commands. would someone mind writing working code? thx and anyway merry christmas :)
  • Jim B
    Jim B over 12 years
    Have you considered using powershell instead? Should be a long one-liner.
  • Miles Erickson
    Miles Erickson over 12 years
    @Email (1) What error are you receiving? Saying "it does not work" without detail is not constructive, and "would someone mind writing working code?" is insulting. (2) Is 7z.exe's program directory in %PATH%?
  • ksoo
    ksoo over 8 years
    Great answer! One of the best I've ever seen. Thoroughly, yet clearly and concisely, explained. Awesome job!
  • Austin
    Austin over 7 years
    Would it be hard to expand the script so I can add a hard coded pathway for all the folders to be extracted too? So rather than extracting in current location, extract to a new folder pathway?
  • Stephen Jennings
    Stephen Jennings over 7 years
    @Austin I don't have access to it right now, but 7-Zip comes with an excellent help file. There's a section for everything 7-Zip can do from the command line, and I bet what you want is there.
  • Thought
    Thought about 7 years
    Note that this won't work if the directories or filenames have space or tab in them. To work with those, you need to specify the option "delims=" to remove those as token delimiters.
  • acruns
    acruns over 6 years
    for /F "DELIMS=" %%I IN ('dir /b /s *.zip *.rar') DO ( "C:\Program Files\7-Zip\7z.exe" x -aos -o"%%~dpI" "%%I" )
  • Admin
    Admin almost 2 years
    Thanks a lot for sharing this. Could you please also share further code changes to modify script to process/extract a directory & its sub-directories without any pop-up (or user input) by automatically selecting "No" if in case file to be extracted is already present. Currently we keep getting these pop-ups to provide user inputs if in case file to be extracted is already present in a directory/sub-directory.