4

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"
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
    
  • 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 Jun 30 '17 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.