简体   繁体   中英

Windows batch script to unzip files in a directory

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" 
)

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" 
)

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"
)

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

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 /?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM