Mark Kennedy
Mark Kennedy

Reputation: 1781

Windows batch script to unzip files in a directory

I want to unzip all files in a certain directory and preserve the folder names when unzipped.

The following batch script doesn't quite do the trick. It just throws a bunch of the files without putting them into a folder and doesn't even finish.

What's wrong here?

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

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

Upvotes: 11

Views: 103100

Answers (5)

kopagl
kopagl

Reputation: 3

As PowerShell script (and without a 3rd party tool) you can run:

#get the list of zip files from the current directory
$dir = dir *.zip
#go through each zip file in the directory variable
foreach($item in $dir)
  {
    Expand-Archive -Path $item -DestinationPath ($item -replace '.zip','') -Force
  }

Taken from Microsoft forum posted by user 'pestell159'.

Upvotes: 0

Billy Scott
Billy Scott

Reputation: 71

Ansgar's response above was pretty much perfect for me but I also wanted to delete archives afterwards if extraction was successful. I found this and incorporated it into the above to give:

for /R "Destination_Folder" %%I in ("*.zip") do (
  "%ProgramFiles%\7-Zip\7z.exe" x -y -aos -o"%%~dpI" "%%~fI"
  "if errorlevel 1 goto :error"
    del "%%~fI"
  ":error"
)

Upvotes: 7

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200233

Try this:

for /R "C:\root\folder" %%I in ("*.zip") do (
  "%ProgramFiles(x86)%\7-Zip\7z.exe" x -y -o"%%~dpI" "%%~fI" 
)

or (if you want to extract the files into a folder named after the Zip-file):

for /R "C:\root\folder" %%I in ("*.zip") do (
  "%ProgramFiles(x86)%\7-Zip\7z.exe" x -y -o"%%~dpnI" "%%~fI" 
)

Upvotes: 36

foxidrive
foxidrive

Reputation: 41234

Try this.

@echo off
for /F "delims=" %%I IN (' dir /b /s /a-d *.zip ') DO (
    "C:\Program Files (x86)\7-Zip\7z.exe" x -y -o"%%~dpI\%%~nI" "%%I" 
)
pause

Upvotes: 1

RGuggisberg
RGuggisberg

Reputation: 4750

Is it possible that some of your zip files have a space in the name? If so your 1st line should be:

for /F "usebackq" %%I IN (`dir /b /s "*.zip"`) DO (

Note the use of ` instead of ' See FOR /?

Upvotes: 0

Related Questions