Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

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-:

Windows:

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

Linux:

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

Your Answer

 
discard

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.