12

I would like to write a bat script to do the following:

Use 7 Zip to extract files from an existing zip file, into a folder by the same name as the original zip file (bar the .zip extension), and keeping the file & directory structure that was contained in the zip file.

I can extract all the same files into the current directory, by using

"C:\Program Files (x86)\7-Zip\7z.exe" e  myZipFile.zip
Harriet
  • 1,633
  • 5
  • 22
  • 36
  • 1
    @Harriet On using 7-Zip from command line it is always advisable to open help of 7-Zip - the file `7-zip.chm` in program files folder of 7-Zip - and read in help on tab __Contents__ the pages listed under list item *Command Line Version*. The commands `e` and `x` are both explained in help as well as all other commands and switches. You would have just needed to read the help. – Mofi Jan 31 '17 at 07:02

2 Answers2

8

Just use the command: 7z x *.zip -o\*

Mofi
  • 46,139
  • 17
  • 80
  • 143
Isaac Júnior
  • 97
  • 1
  • 3
  • 2
    I'm not sure that's going to do what he wants. The `-o` option needs a specific directory named. Using `-o\*` is either going to expand to all files (and probably error) or it will create a directory actually named `*` (also not what he wants). – seanahern Jul 31 '20 at 21:11
  • 1
    Replace `*.zip` with actual name of the zip file and it works great. `7z e yourfile.zip -o\*` – blisstdev May 25 '21 at 18:11
  • 1
    `find . -name "*.zip" -exec 7z x "{}" -o\* \;` based on the answer and comments – Martian2020 Oct 22 '21 at 04:51
7

Reading the help of the 7z-command by just typing "C:\Path To\7-Zip\7z.exe" gets the help with all possible arguments. Here we find the following interesting ones:

 e : Extract files from archive (without using directory names)

and

x : eXtract files with full paths

Trial and error shows that the latter is the one fitting your desired behaviour without bigger effort :)

After the comment by @BadmintonCat here is the addition that will create a folder to zip everything into (use as batch script with the file as argument):

@echo off

SET "filename=%~1"
SET dirName=%filename:~0,-4%

7z x -o"%dirName%" "%filename%"

From the help: -o{Directory} : set Output directory. 7z will create the directory if it does not already exist.

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54