如何在 Windows 7 上使用 7-Zip 提取子資料夾中的所有 ZIP 文件

如何在 Windows 7 上使用 7-Zip 提取子資料夾中的所有 ZIP 文件

我有一個大型 Windows 7 資料夾樹結構,其中包含許多壓縮文件。這些是單層 ZIP 檔案(不是 ZIP 中的 ZIP)。我可以使用什麼 7-Zip 命令來解析此文件夾結構,按文件擴展名查找每個 ZIP 文件(參見示例),將其解壓縮(刪除 ZIP 文件,保留解壓的文件)到同一位置?

範例:資料夾層次結構中的所有檔案的命名方式如下:abc.mp3.zip 或 xyz.jpg.zip - 本機檔案副檔名後面跟著「.zip」。我希望7-Zip 使用通配符(*.mp3.zip、*.jpg.zip 等)按檔案副檔名尋找樹中的所有文件,並將這些檔案提取到當前位置,而不建立新資料夾,以便結果為abc。

答案1

據我所知,7-zip 沒有一個命令可以完成您正在尋找的任務。這是一個 Windows 批次檔腳本,我認為它可以滿足您的需求。它應該從命令列運行,以便您可以提供要處理的資料夾樹的根目錄的路徑。

文件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

由於作為一個整體,該腳本所做的事情相當激進,並且可能具有破壞性,因為它可能會提取大量文件並隨後刪除許多zip 文件,因此我禁用了第12 行和第14 行中的命令,這些命令將透過在它們前面加上echo.這使得他們只需列印出如果echo沒有的話他們會做什麼。這樣,如果出現某種意外問題,您可以先測試腳本,而不會損壞檔案系統。

要修改腳本來實際執行這些操作,您需要刪除echo這兩行中的每一行。當然,標準免責聲明適用。

相關內容