1

I've got hundreds sub-directories, with each sub-directory having 1 or more archives in them. I want to recursively extract every archive in every sub-directory, with the extracted files ouputted in each sub-directory.

Starting with this:

DIR1
    SUBDIR2
    --->ZipFileA
    --->ZipFileB
    SUBDIR3
    --->ZipFileC
    --->ZipFileD

Want to end up with this:

DIR1
    SUBDIR2
    --->ExtractedZipFileA
    --->ExtractedZipFileB
    SUBDIR3
    --->ExtractedZipFileC
    --->ExtractedZipFileD

How do I do this?

I've tried...

7z x E:\Downloads\New -r -o*

... but this outputs everything in the main directory, rather than outputting them in their own sub-directories.

2
  • Could you use PowerShell instead? E.g. dir -Recurse -file -filter *.zip | %{Expand-Archive $_.FullName $_.PSParentPath} Commented Nov 11, 2020 at 20:17
  • Thanks so much! This worked, although since my archives were CBZ rather than ZIP and since PowerShell doesn't extract ZIP archives with the CBZ extension, I needed to do for /R %x in (*.CBZ) do ren "%x" *.ZIP first
    – xkmasada
    Commented Nov 12, 2020 at 5:17

2 Answers 2

0

Working like a charm:

for /r %a in (*.zip) do 7z.exe e "%~a" -o"%~dpa"
2
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Aug 25, 2022 at 10:53
  • what the hell are the arguments for 7z.exe? please expand your answer. the expansion of the commandline is not in the documentation
    – WhiteRau
    Commented Oct 18, 2023 at 22:09
0

I found the above listed:

for /r %a in (*.zip) do 7z.exe e "%~a" -o"%~dpa"

to expand everything into the current directory. Not exactly what I was looking for anyways.

So, assuming I am in a working directory which contains a number .zip files and each of those zip files contain additional zip files etc then the following will expand them all retaining the the paths:

for /r %A in ("*.zip") do 7z.exe x "%~dfA" -o"%~pnA"

To explain this there are two parts,

  • search for files based on criteria
  • for each file found do something

First part, search for /r %A ("*.zip")

search for files matching ("*.zip") and for each file found replace the variable %A with the filename then pass it to the second part ...I skipped over /r which is the recurse flag

second part, do something (so for each file found do...)

do 7z.exe x "%~dfA" -o"%~pnA"

do 7z.exe x - do 7zip extract
then two arguments for 7z
"%~dfA" : "%-df" bit gets the drive letter and fully qualified path for whatever follows it, in this case A follows it, which is a reference to our earlier declared A and is the file being passed to the do

-o"%~pnA" : -o is the 7zip output flag "%~pn extracts the path and filename (w/o ext) from whatever follows it, again it's A
example
given file: c:\test\foo.zip being passed to: -o"%~pnA"
"%-p" would extract the path from the file, *c:\test*
"n" would extract the simple filename, foo
resulting in c:\test\foo

Reference https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/for

You must log in to answer this question.

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