1

I solved this problem, see answer below. Now my question is, how to make it tolerate directories with spaces in the dir name.


I'm looking for a command line for 7zip that achieves the following: I have a base folder, containing many subfolders, each of which contains many zip files. Each zip file in turn contains many files. Like this:

Before:

 c:\base\dir1
       \dir1\x1.zip  
            \x1.zip\file1.txt 
            \z1.zip\file2.txt 

       \dir1\x2.zip    
            \x2.zip\file1.txt 
            \x2.zip\file1.txt

c:\base\dir2\

     \dir2\z1.zip
          \z1.zip\file1.txt 
          \z1.zip\file2.txt  

     \dir2\z2.zip
          \z2.zip\file1.txt 
          \z2.zip\file2.txt 

[the same structure repeated for dir3, dir4...dirn]

What I'm trying to achieve is this.

After:

c:\base\dir1
       \dir1\x1   
            \x1\file1.txt 
            \x1\file1.txt 

       \dir1\x2   
            \x2\file1.txt 
            \x2\file1.txt 

c:\base\dir2\        
       \dir2\z1
            \z1\file1.txt 
            \z1\file2.txt 
       \dir2\z2
            \z2\file1.txt 
            \z2\file2.txt 

In other words, each of the zip files have been unzipped into subfolders with the same name as the zip file. Those subfolders are in the same folder that each zip file was in.

I have tried this:

for /F "usebackq" %f in (dir /b/a:d) do C:\APPS\7-Zip\7z.exe x .%f*.zip -o%f

But this places all the unzipped files in the folder the zip is in (x1.zip gets unzipped to c:\base\dir1, z1.zip gets unzipped to c:\base\dir2, etc.) rather than into subfolders No subfolders named after the zip file are created. IOW this:

Incorrect 1:

c:\base\dir1
       \dir1\file1.txt 
       \dir1\file2.txt 

etc. I also tried this:

for /F "usebackq" %f in (`dir /b/a:d`) do "C:\Program Files\7-Zip\7z.exe" x .\%f\*.zip -o*

Now subfolders named after the name of the zip file are indeed created, but all of them are in c:\base, like this:

Incorrect 2:

c:\base
  \base\x1\file1.txt
  \base\x2\file2.txt

etc.

Again, what I'm going for is:

c:\base
  \base\dir1\x1\file1.txt
  \base\dir2\z1\file1.txt

Sorry for this very longwinded post about what seems to be a fairly simple operation. I really struggled getting the formatting of my sample folder trees to look right, so apologies if they're confusing. If anyone knows how to do this, hopefully with the right command line, I'll be grateful.

1
  • Additional comment: the -o switch tells 7zip where to unzip to, so -o$f in this context unzips into the folder containing the zip file. -o* causes 7zip to unzip into new folders named after the zip file. I could find no way to combine these two operations: unzip into a new subfolder inside a specified folder.
    – Robert M.
    Sep 27, 2022 at 0:57

1 Answer 1

0

I figured it out. It's just:

for /F "usebackq" %f in (`dir /b/a:d`) do "C:\Program Files\7-Zip\7z.exe" x .\%f\*.zip -o%f\*

-o%f* creates a subfolder named after the zip file inside the folder the zip file is in.

Another question though, how can I make it tolerate directories with spaces in them?

You must log in to answer this question.

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