5

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"
1

2 Answers 2

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
    
1
  • 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
    Commented Jun 30, 2017 at 7:21
1

Why not use one for /Recursive to all extensions you need?

@echo off 

For /R "%userprofile%\desktop\test" %%i in (*.Zip,*.7z,*.Rar
)do "c:\program files\7-zip\7z.exe" x "%%~dpnxi" -o"%%~dpi\"
goto :EOF

FOR /R
Loop through files (Recurse subfolders)

Syntax
      FOR /R [[drive:]path] %%parameter IN (set) DO command

Key
   drive:path  : The folder tree where the files are located.

   set         : A set of one or more files enclosed in parentheses (file1.*, another?.log).
                 Wildcards must be used.
                 If (set) is a period character (.) then FOR will loop through every folder.

   command     : The command to carry out, including any parameters.
                 This can be a single command, or if you enclose it
                 in (parentheses), several commands, one per line.

   %%parameter : A replaceable parameter:
                 in a batch file use %%G (on the command line %G)
This command walks down the folder tree starting at [drive:]path, and executes the DO statement against each matching file.

If the [drive:]path are not specified they will default to the current drive:path.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .