用于将文件解压到目录中的 Windows 批处理脚本

问题描述 投票:0回答:5

我想解压某个目录下的所有文件,并保留解压时的文件夹名称。

以下批处理脚本并不能完全解决问题。它只是抛出一堆文件,而不将它们放入文件夹中,甚至没有完成。

这里出了什么问题?

for /F %%I IN ('dir /b /s *.zip') DO (

    "C:\Program Files (x86)\7-Zip\7z.exe" x -y -o"%%~dpI" "%%I" 
)
windows batch-file cmd unzip
5个回答
34
投票

试试这个:

for /R "C:\root\folder" %%I in ("*.zip") do (
  "%ProgramFiles(x86)%\7-Zip\7z.exe" x -y -o"%%~dpI" "%%~fI" 
)

或者(如果您想将文件解压到以 Zip 文件命名的文件夹中):

for /R "C:\root\folder" %%I in ("*.zip") do (
  "%ProgramFiles(x86)%\7-Zip\7z.exe" x -y -o"%%~dpnI" "%%~fI" 
)

7
投票

Ansgar 的上述回复对我来说非常完美,但如果提取成功,我也想在之后删除档案。我找到了this并将其合并到上面给出:

for /R "Destination_Folder" %%I in ("*.zip") do (
  "%ProgramFiles%\7-Zip\7z.exe" x -y -aos -o"%%~dpI" "%%~fI"
  "if errorlevel 1 goto :error"
    del "%%~fI"
  ":error"
)

1
投票

试试这个。

@echo off
for /F "delims=" %%I IN (' dir /b /s /a-d *.zip ') DO (
    "C:\Program Files (x86)\7-Zip\7z.exe" x -y -o"%%~dpI\%%~nI" "%%I" 
)
pause

0
投票

您的某些 zip 文件的名称中是否有可能包含空格?如果是这样,你的第一行应该是:

for /F "usebackq" %%I IN (`dir /b /s "*.zip"`) DO (

注意使用 ` 而不是 ' 看到/?


0
投票

作为 PowerShell 脚本(无需第三方工具),您可以运行:

#get the list of zip files from the current directory
$dir = dir *.zip
#go through each zip file in the directory variable
foreach($item in $dir)
  {
    Expand-Archive -Path $item -DestinationPath ($item -replace '.zip','') -Force
  }

取自微软论坛,由用户“pestell159”发布。

© www.soinside.com 2019 - 2024. All rights reserved.