Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I know this has to be so easy, but I do not know how to do it. Any help would be appreciated.

I want to name zip files with the directory name.

Say I have a directory "Zip1" with files "Doc1.txt" and "Doc2.txt"

I understand everything about zipping the folder with the exception of naming it. I can find NO examples of how to name a file through the command line using directory name information.

How can I name this zip file as "Zip1.7z" without actually typing the directory name but having the command line pull this information from the directory the files reside in?

Can anyone help?

share|improve this question

I assume that you are in a directory where all subdirectories should be zipped and that the 7-Zip binaries are on your PATH.

You can then use the following on Windows (directory is %i):

for /d %i in (*) do 7z ... %i.7z ...

On Linux (directory is $i):

for i in $(find -mindepth 1 -maxdepth 1 -type d) ; do 7zr ... $i.7z ... ; done

Shorter but less robust:

for i in */ ; do 7zr ... ${i%/}.7z ... ; done

Example to zip all .txt files in all directories which start with backup-:


for /d %i in (backup-*) do 7z ... %i.7z %i\*.txt


for i in backup-*/ ; do 7zr ... ${i%/}.7z $i/*.txt ; done
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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