0

I am trying to create a batch file to recursively extract multiple archives in multiple sub-directories from a source directory to an output directory while keeping archive folder structure. The below example works perfectly EXCEPT that I need to extract only one specific file type from each archive (i.e., .txt., .doc, .pdf, etc.) What do I need to change in order to make this work?

@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"C:\Users\user\Desktop\Working\Custom Textures\*"
FOR /F "TOKENS=*" %%F IN ('DIR /S /B "%source%\*.7z"') DO "C:\Program Files\7-Zip\7z.exe" x "%%~fF" -o"C:\Users\user\Desktop\Working\Custom Textures\*"
FOR /F "TOKENS=*" %%F IN ('DIR /S /B "%source%\*.rar"') DO "C:\Program Files\7-Zip\7z.exe" x "%%~fF" -o"C:\Users\user\Desktop\Working\Custom Textures\*"
PAUSE

I sincerely appreciate the help in advance, and any thorough resources on batch scripting that I could be directed to for future references would be greatly appreciated as well, as I am trying to learn.

3
  • @Worthwelle: This says windows batch-file, not linux shell-script. # is not a valid comment character. Instead either use the REM command, or start the line with :: (or you could just delete the offending lines).
    – 3D1T0R
    Sep 24, 2018 at 21:23
  • Yup, I managed to confuse myself. Let's try this again: If you only need to extract *.7z files, then you can comment out (using :: or REM) or remove the first and third FOR loops (lines 4 and 6).
    – Worthwelle
    Sep 24, 2018 at 21:34
  • My apologies. I should have worded myself better. I do want each archive extension as above, but what I need is from each archive to only extract one file type, i.e. .txt, .doc, etc.
    – MorbidEden
    Sep 24, 2018 at 21:43

1 Answer 1

1

You can use the -i switch with 7-Zip for this. As an example, the following modification of your batch file will only extract .txt files.

@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"C:\Users\user\Desktop\Working\Custom Textures\*" -ir!*.txt
FOR /F "TOKENS=*" %%F IN ('DIR /S /B "%source%\*.7z"') DO "C:\Program Files\7-Zip\7z.exe" x "%%~fF" -o"C:\Users\user\Desktop\Working\Custom Textures\*" -ir!*.txt
FOR /F "TOKENS=*" %%F IN ('DIR /S /B "%source%\*.rar"') DO "C:\Program Files\7-Zip\7z.exe" x "%%~fF" -o"C:\Users\user\Desktop\Working\Custom Textures\*" -ir!*.txt
PAUSE

Here is some more information from the documentation:

-i (Include filenames) switch

Specifies additional include filenames and wildcards.

Multiple include switches are supported.

Syntax

-i[<recurse_type>]<file_ref>

<recurse_type> ::= r[- | 0]
<file_ref> ::= @{listfile} | !{wildcard}

Parameters

<recurse_type>

Specifies how wildcards and file names in this switch must be used. If this option is not given, then the global value, assigned by the -r (Recurse) switch will be used. For more details see specification of the -r (Recurse) switch.

<recurse_type> ::= r[- | 0]

<file_ref>

Specifies filenames and wildcards, or a list file, for files to be processed.

<file_ref> ::= @{listfile} | !{wildcard}

Option        Description
{listfile}    Specifies name of list file. See List file description.
{wildcard}    Specifies wildcard or filename.

Examples

7z a -tzip src.zip *.txt -ir!DIR1\*.cpp

adds to src.zip archive all *.txt files from current directory and all *.cpp files from directory DIR1 and from all it's subdirectories.

0

You must log in to answer this question.

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