Friday 8 February 2019

cmd.exe - Batch extract files from every archive with 7-zip


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"

Answer



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



No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...