1

I have a 7zip archive that has several files/folders that I want to extract to a specific place. I've simplified the example to make it minimal, complete, and verifiable.

-I don't want to maintain the directory structure, so I'm using the e command. I just want all the files (in all the directories) to go to one place.

-I have a specific output directory, so I'm also using -o.

A restriction is that I'm unable to alter the original archive (it would involve another team and I would prefer not to do that). I would ideally like to not have to hard code directories to remove after I extract the archive.

The problem comes after everything is extracted -- I'm left with some empty folders in my output directory. Is there a way for 7zip to either remove these empty directories or not create them in the first place?

My 7zip command line looks like:

D:\7z.exe e D:\MyArchive.zip -oD:\ExtractToHere

An example .zip file would have a structure like:

./A/1.txt
./B/2.txt
./C/3.txt
./D/fileWithNoExt

What I want in my output directory is (and nothing else):

./1.txt
./2.txt
./3.txt
./fileWithNoExt

What I have in my output directory is (A, B, and C are all empty directories):

./1.txt
./2.txt
./3.txt
./fileWithNoExt
./A/
./B/
./C/
./D/
  • @LMFAO_A_JOKE The logic makes sense -- what I'm really looking for here though was a way to do this with 7zip. I would keep the answer as it does still add value. Cheers. – PerryC Jan 11 '16 at 15:08
1
+50

There is only one way I can think of, and it does have pitfalls

Your current command, but include:

D:\7z.exe e D:\MyArchive.zip -oD:\ExtractToHere *.* -r

The problems with this though are that

  1. Files without an extension wouldn't be extracted
  2. Directories with a . in the middle of them would still get extracted

For your example structure above it should work as intended, but I don't know your actual requirements. I don't believe there is a way to exclude folders specifically.

  • I have a feeling like that's as close as we're going to get. It's unfortunate that you can't replace *.* with just plain * and have it only apply to files. – PerryC Jan 8 '16 at 17:58
  • @PerryC Indeed - I don't think the differentiation can be made without somehow getting an attribute for a directory. Quite frustrating though, I agree. In a Linux/Mac setting this solution would result in more problems than it solves, but if kept within a Windows environment it would be fairly reliable. Still, I'd feel safer just doing a second sweep with another utility or script afterwards to "tidy up". – Jonno Jan 8 '16 at 18:02
1

Can 7zip remove empty directories after extracting an archive?

I would ideally like to not have to hard code directories to remove after I extract the archive.

I'm left with some empty folders in my output directory. Is there a way for 7zip to either remove these empty directories or not create them in the first place?

I tested and confirmed the below logic works as expected for removing the empty directories beneath your extract directory with the 7Zip commands you are running currently.

You'd just have to set your source file and extract location, and then copy and paste that into the command prompt to do exactly what you've explained.

  • I'm not sure what all concerns you with hard-coding the extract directory and zip file name(s) to extract, but your example shows that you are doing just this. So this below method will include no more effort than that just as your example shows.

Additionally, with this method you can leave your 7Zip commands just as you are using them now.

This confrimed working example below can further be expanded to loop through all zip files in a particular folder, if needed, rather than hard-coding explicitly one way or another for how you were doing, etc. I'll be happy to add an example with this as well, so just let me know.


Example Working Command Prompt Logic

@ECHO ON
SET SourceFile=D:\MyArchive.zip
SET ExtractDir=D:\ExtractToHere

D:\7z.exe e %SourceFile% -o%ExtractDir%

FOR /F "TOKENS=*" %A IN ('"DIR /S /B /AD "%ExtractDir%""') DO RD "%~A"

Further Resources and Reading

0

If you can run another command after unzipping, you could just use robocopy to delete the empty folders

 ROBOCOPY folderName folderName /S /MOVE

source: https://stackoverflow.com/questions/7831286/how-to-delete-empty-folders-using-windows-command-prompt

  • I'm sure there are many good ways to delete empty folders, ROBOCOPY being one of them, but I'm ideally looking for a way for 7zip to not create this problem in the first place. – PerryC Jan 8 '16 at 16:33
0

If a two-pass approach is viable, you could list the contents with 7z l, and parse out the empty directories. From there you can either include what you want with -i or exclude what you don't with -x depending on which is going to be more efficient for you.

  • That would add a layer of complication that I don't want. This example IRL is being used in a .proj file running through MSBuild. Currently, there is an extraction, a move operation, and then a delete happening. I'm simply trying to see if I can reduce the complexity. – PerryC Jan 8 '16 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.