How to write a batch file that can unzip a file into a new folder with the same name?

前端 未结 1 1125
终归单人心
终归单人心 2020-12-22 01:07

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

相关标签:
1条回答
  • 2020-12-22 01:49

    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.

    0 讨论(0)
提交回复
热议问题