Batch extract files from every archive with 7-zip

4

2

I want to use this code in a batch. First step is to input source folder which tree would look like this:

enter image description here

What I want to achieve is to extract every type of archive within the source folder to the same folder where the archive is located, eg. "archive.zip" directly to "folder1".

Have this code below but don't have any clue how to set the destination variable.

SET /P "source="

FOR %%F IN ("%source%\*.zip") DO "C:\Program Files\7-Zip\7z.exe" x "%source%\%%~nF.zip"
FOR %%F IN ("%source%\*.7z") DO "C:\Program Files\7-Zip\7z.exe" x "%source%\%%~nF.7z"
FOR %%F IN ("%source%\*.rar") DO "C:\Program Files\7-Zip\7z.exe" x "%source%\%%~nF.rar"

Rayearth

Posted 2017-06-29T20:28:24.907

Reputation: 123

Answers

2

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
    

Pimp Juice IT

Posted 2017-06-29T20:28:24.907

Reputation: 29 425

This works flawlessly of course - many thanks for the explanation on how to approach this problem. BTW. do you have any idea why 7-Zip switches -y (assume yes on all queries) and -aoa (overwrite All existing files without prompt) don't work and I'm being ask every time what to do when there's a file name conflict? – Rayearth – 2017-06-30T07:21:52.573