Batch extract files from every archive with 7-zip

Recursively extract from archive files with 7Zip to the same folder the archive file is located

You can use the -o switch with 7Zip which will specify the full path of the output directory for the extract command to extract the contents of applicable archive files.

You can use a FOR /F loop with a recursive DIR command to iterate the full archive paths and pass those over to 7Zip accordingly using substitutions to get it to work as you need it to.

Batch Script

@ECHO ON

SET source=C:\Users\User\Desktop\Test
FOR /F "TOKENS=*" %%F IN ('DIR /S /B "%source%\*.zip"') DO "C:\Program Files\7-Zip\7z.exe" x "%%~fF" -o"%%~pF\"
FOR /F "TOKENS=*" %%F IN ('DIR /S /B "%source%\*.7z"') DO "C:\Program Files\7-Zip\7z.exe" x "%%~fF" -o"%%~pF\"
FOR /F "TOKENS=*" %%F IN ('DIR /S /B "%source%\*.rar"') DO "C:\Program Files\7-Zip\7z.exe" x "%%~fF" -o"%%~pF\"
EXIT

Further Resources

  • DIR
  • FOR /F

    In addition, substitution of FOR variable references has been enhanced. You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string