问题
I want to unzip all files in a certain directory and preserve the folder names when unzipped.
The following batch script doesn't quite do the trick. It just throws a bunch of the files without putting them into a folder and doesn't even finish.
What's wrong here?
for /F %%I IN ('dir /b /s *.zip') DO (
"C:\Program Files (x86)\7-Zip\7z.exe" x -y -o"%%~dpI" "%%I"
)
回答1:
Try this:
for /R "C:\root\folder" %%I in ("*.zip") do (
"%ProgramFiles(x86)%\7-Zip\7z.exe" x -y -o"%%~dpI" "%%~fI"
)
or (if you want to extract the files into a folder named after the Zip-file):
for /R "C:\root\folder" %%I in ("*.zip") do (
"%ProgramFiles(x86)%\7-Zip\7z.exe" x -y -o"%%~dpnI" "%%~fI"
)
回答2:
Ansgar's response above was pretty much perfect for me but I also wanted to delete archives afterwards if extraction was successful. I found this and incorporated it into the above to give:
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"
)
回答3:
Try this.
@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
回答4:
Is it possible that some of your zip files have a space in the name? If so your 1st line should be:
for /F "usebackq" %%I IN (`dir /b /s "*.zip"`) DO (
Note the use of ` instead of ' See FOR /?
来源:https://stackoverflow.com/questions/17077964/windows-batch-script-to-unzip-files-in-a-directory