
如何在 Windows 上遞歸提取 zip 檔案?每個 zip 檔案都應該解壓縮到一個新的子目錄中,然後刪除該 zip 檔案。
應掃描每個現有或建立的目錄以查找更多現有 zip 文件,等等。
所以問題是,我有一個巨大的 zip 文件,其中包含許多目錄,可能還包含許多其他 zip 文件。原始 zip 檔案應該簡單地從任何 zip 檔案中釋放出來,原始目錄樹應該保留,並且約定每個 zip 檔案應該表示為自己的目錄。
所以背後的邏輯是:在目錄中解壓縮檔案並刪除zip 檔案-> 進入該目錄並以相同的方式解壓縮那裡的所有zip 檔案-> 進入該目錄的每個現有子目錄並執行相同的操作-> 等等遞歸地
遞歸編程批次腳本文件的粗略建議:
unzip_folder(%%directory):
for %%file (%%directory/*.zip) do (unzip %%file | del %%file)
for /d %%directory (*) do ( call unzip_folder(%%directory) )
return
答案1
立即擷取做這個。看http://www.extractnow.com/Usage.aspx#process。請注意,Chrome 和其他瀏覽器可能會將應用程式標記為惡意軟體/間諜軟體。作者堅稱這只是來自安裝程序並指向一個便攜版對於那些不想要安裝程式的人(儘管 Chrome 也稱該 zip 檔案是惡意的)。
答案2
這應該對你有用(對我有用)。請注意,如果還有任何其他資料夾,它將遞歸遍歷它們並解壓縮所有可能的檔案。我的建議:在運行之前將您的 zip 檔案(以及此批次檔)單獨放入一個目錄中。
:: To actually include the path expansion character (tilde), I had to give valid numbers; see http://ss64.com/nt/rem.html for bug reference. Also, try call /? for more info.
@REM The %~n0 extracts the name sans extension to use as output folder. If you need full paths, use "%~dpn0". The -y forces overwriting by saying yes to everything. Or use -aoa to overwrite.
@REM Using `x` instead of `e` maintains dir structure (usually what we want)
:: If you want recursive, use FOR /R
@FOR /R %%a IN (*.zip) DO @(
@if [%1] EQU [/y] (
@7z x "%%a" -o"%%~dpna" -aoa
) else if [%1] EQU [/yd] (
@7z x "%%a" -o"%%~dpna" -aoa
@if errorlevel 1 (
@echo There was an error so I won't delete
) else (
REM You can also prompt with del /p
@del "%%a"
)
) else (
@echo 7z x "%%a" -o"%%~dpna" -aoa
)
)
@echo USAGE: Use /y to actually do the extraction. Use /yd to extract then delete the zip file.
答案3
echo %rant%
好吧,我們開始了......幾個小時的研究、失敗的嘗試和大量擺弄的結果(甚至嘗試過 PowerShell - 不支持開箱即用的長文件路徑 -哎呀!)...一個實際上遞歸提取檔案並將它們全部放在一個資料夾中以便刪除的版本...
我採納了 Pat 的答案並做了很多修改...以支援...長度超過 260 個字元的長檔案路徑!
@echo off
setlocal enabledelayedexpansion enableextensions
set scriptDir=%~dp0
REM Clear the log files.
echo . > unzipLog.txt
echo . > unzipErrors.txt
mkdir DeleteMe >> unzipLog.txt 2>nul
REM Recurse through all common compressed archive files.
FOR /R %%a IN (*.zip,*.7z,*.rar,*.tar,*.gz) DO (
@echo: >> unzipLog.txt 2>> unzipErrors.txt
@echo: >> unzipLog.txt 2>> unzipErrors.txt
@echo: >> unzipLog.txt 2>> unzipErrors.txt
@echo: >> unzipLog.txt 2>> unzipErrors.txt
@echo: >> unzipLog.txt 2>> unzipErrors.txt
REM Prepend \\?\ to the beginning of each path to handle paths longer than 260 characters.
if [%1] EQU [/y] (
REM Extract only.
7z x "\\?\%%a" -o"%%~dpna" -aoa >> unzipLog.txt 2>> unzipErrors.txt
) else if [%1] EQU [/yd] (
REM Extract and delete.
for %%b in ("%%a") do (
set p=%%~dpb
set f=%%~nxb
)
IF !p:~-1!==\ SET p=!p:~0,-1!
echo "!p!" "!scriptDir!DeleteMe" "!f!"
echo "!p!" "!scriptDir!DeleteMe" "!f!" >> unzipLog.txt 2>> unzipErrors.txt
7z x "\\?\%%a" -o"%%~dpna" -aoa >> unzipLog.txt 2>> unzipErrors.txt
if errorlevel 1 (
echo There was an error so I won't delete >> unzipLog.txt 2>> unzipErrors.txt
) else (
robocopy "!p!" "!scriptDir!DeleteMe" "!f!" /MOVE /FP /NS /NC /NFL /NDL /NP /IS /IT /SL >> unzipLog.txt 2>> unzipErrors.txt
)
) else (
REM Just echo.
echo 7z x "\\?\%%a" -o"%%~dpna" -aoa >> unzipLog.txt 2>> unzipErrors.txt
)
)
REM Can comment this out if you just want to extract the archives to a folder and not delete them...:
REM WARNING: recommended call this manually and very carefully!!!
REM rmdir /S /Q DeleteMe
REM WARNING: recommended call this manually and very carefully!!!
echo Use /y to actually do the extraction. Use /yd to extract then delete the zip file.
echo See unzipLog.txt and unzipErrors.txt!
endlocal
答案4
自從 Andrew 的 '''setx PATH "%PATH%;C:\Program Files\7-Zip"''' 由於某種原因沒有採用以來,修改了 Pat:
@FOR /R %%a IN (*.zip) DO @(
@if [%1] EQU [/y] (
@"C:\Program Files\7-Zip\7z.exe" x "%%a" -o"%%~dpna" -aoa
) else if [%1] EQU [/yd] (
@"C:\Program Files\7-Zip\7z.exe" x "%%a" -o"%%~dpna" -aoa
@if errorlevel 1 (
@echo There was an error so I won't delete
) else (
REM You can also prompt with del /p
@del "%%a"
)
) else (
@echo "C:\Program Files\7-Zip\7z.exe" x "%%a" -o"%%~dpna" -aoa
)
)
@echo USAGE: Use /y to actually do the extraction. Use /yd to extract then delete the zip file.
在 .zip 檔案路徑中的 cmd.exe 中,使用「unzipRecursively0.bat /yd」來運行它。