2

I have a large Windows 7 folder tree structure containing many ZIP files. These are single-layer ZIP files (not ZIP within ZIP). What 7-Zip command can I use to parse through this folder structure, find each the ZIP file by file extension (see example), extract it (deleting the ZIP file, leaving the extracted files) to the same location?

Example: All of the files in the hierarchy of folders are named something like: abc.mp3.zip or xyz.jpg.zip - the native file extension followed by ".zip". I want 7-Zip to find all files in the tree by file extension using wildcards (*.mp3.zip, *.jpg.zip, etc) and extract these to the current location without creating new folders so that the result is abc.mp3 and xyz.jpg.

1
  • 3
    There is no 7-zip "command" for something that intricate, you'll want to write a script.
    – user1931
    Feb 2, 2011 at 15:57

1 Answer 1

2

As far as I can tell, 7-zip doesn't have a command that does what you're looking for. Here's a Windows batch file script which I think will do what you want. It should be run from the command line so you can supply a path to the root of the folder tree to be processed.

File unzipper.bat

@echo off
setlocal
if "%1"=="" goto Usage

call :Get7zCmd
:: Recurse folder passed in as paramater
for /r %1 %%Z in (*.zip) do (
    echo ====
    rem Change to the directory of zip file
    cd /d "%%~dpZ"
    rem Extract all files to current directory
    echo %_7zCmd% e "%%~nxZ" -y
    rem Delete the zip file
    echo del "%%~nxZ"
)
goto End

:Usage
echo.
echo Parses through folder structure starting at the specified path, finding
echo and extracting the contents of all zip files found, and then deletes
echo the zip file.
echo.
echo Usage:
echo     %~n0 root-directory-path
echo.
echo     For example:
echo.
echo %~n0 "D:\some folder"

:End
goto :EOF

:: ==========================
:: Subroutine Get7zCmd
:: Determines the full path to 7-zip command-line executable from the Windows
:: Registry and sets the variable "_7zCmd" to the result.
:Get7zCmd
set Reg.Key=HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\7zFM.exe
set Reg.Val=Path
for /F "Tokens=2*" %%A in ('Reg Query "%Reg.Key%" /v "%Reg.Val%" ^| find /I "%Reg.Val%"') do call set PathDirectory=%%B
set _7zCmd="%PathDirectory%%\7z.exe"
exit /b 0

Since as a whole what the script does is fairly radical and potentially destructive because it might extract large numbers of file and delete many zip files afterward, I've disabled the commands on lines 12 and 14 which would do those things by prefixing them with echo. This make them just print out what they would have done if the echo wasn't there. That way you can test the script first without doing any damage to your file system should there be some sort of unexpected problem.

To modify the script actually perform these actions, you'll need to remove the echo on each of the two lines. Standard disclaimer of any liability applies, of course.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .