1

I need to write a batch script that will allow me to unzip a .zip file into a new folder that has the same name as the zip file. I cannot assume that I have some program to unzip the file; it has to work as if the computer is brand new.

For example, if I have a group of zip files named a1, b2, c3, and d4, I want their contents to go into 4 different folders labeled a1, b2, c3, d4. It doesn't need to be concise, it just has to work.

Seanzies93
  • 321
  • 1
  • 6
  • 12

1 Answers1

1

Read excellent topic How can I compress (zip) and uncompress (unzip) files and folders with batch file without using any external tools? written by npocmaka.

I suggest to use the second solution: Using Shell.Application

The task can be done with zipjs.bat also written by npocmaka with a batch file with just a few lines:

@echo off
for %%I in (*.zip) do call "%~dp0zipjs.bat" unzip -source "%%~fI" -destination "%%~dpnI\" -force yes
for /F "delims=" %%D in ('dir /AD /B "%TEMP%\*.zip" 2^>nul') do (
    %SystemRoot%\System32\attrib.exe -h "%TEMP%\%%~D"
    rd /S /Q "%TEMP%\%%~D" 2>nul
)

The hybrid batch file zipjs.bat must be in same folder as the batch file with the two lines above.

Running in a command prompt window call /? explains %~dp0 which means drive and path of argument 0 of batch file without surrounding quotes ending always with a backslash. Argument 0 of a called batch file is the name of the batch file, see also What does %~dp0 mean, and how does it work?

Help of command for displayed on running in a command prompt window for /? explains

  • %%~fI - full file/folder name without surrounding quotes, and
  • %%~dpnI - drive, path and name of a file/folder without surrounding quotes and without extension

for loop variables like call /? does for batch parameters/arguments.

The second for loop finds all subdirectories in directory for temporary files with .zip in directory name, removes the hidden attribute and deletes those subdirectories. This is necessary as zipjs.bat first extracts each file into a newly created subdirectory in directory for temporary files and leaves them there after successful extraction.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143