Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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" 
)
like image 295
Mark Kennedy Avatar asked Jun 13 '13 01:06

Mark Kennedy


People also ask

How do I unzip a file location?

To unzip files Open File Explorer and find the zipped folder. To unzip the entire folder, right-click to select Extract All, and then follow the instructions. To unzip a single file or folder, double-click the zipped folder to open it. Then, drag or copy the item from the zipped folder to a new location.

How do I zip a folder in Windows 10 using CMD?

Use the Windows Command Prompt to Make ZIP FilesUse the cd command to go to the folder where your files are located. Enter the following command in the Command Prompt window and hit Enter. Replace output. zip with the name you want to give your ZIP file, and myfile.


1 Answers

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"
)
like image 153
Billy Scott Avatar answered Oct 25 '22 06:10

Billy Scott