如何在 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
修改了 Pat 的,因为 Andrew 的 '''setx PATH "%PATH%;C:\Program Files\7-Zip"''' 由于某种原因没有被采用:
@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”运行它。