Search code examples
batch-filezipunzip

Batch script to unzip files into a certain folder


I have multiple .zip files which I want to unzip by a script. After a short research. I have this script:

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

The problem is, that it only unzips the files into the same folder. I have a designated folder for the unzipped files, can't get the script to move the files into that folder. Anyone has an idea what I need to add to this script?


Solution

  • When reading the help for 7z in your cmd window, you will notice that the -o switch is the output directory option. Currently you are telling it to be %%~dpI which is in fact Drive and Path of the current of the zip files. So you would want to change the output directory:

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

    I do not currently have 7zip installed, but I am almost 100% sure that it has a recursive function builtin, if so, you do not even need the for loop, you can just try:

    7z.exe x -y "C:\root\folder\*.zip" -oc:"C:\root\folder2" -r
    

    If it does not work, I will remove this section from the answer.