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.
FOR %i IN (*.*) DO 7z.exe a "%~ni.7z" "%i"

Does the job for each selected file.

However I've got tons of directories (with files inside them obviously) I need to pack.

Say I have d:\dir1, d:\dir2, d:\dir3, d:\dir4. I need 7zip to pack them this way:

e:\dir1.7z, e:\dir2.7z, e:\dir3.7z, e:\dir4.7z.

How do I do that in 7zip command line?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

From the command prompt you could use something like:

FOR /D %i IN (d:\dir*.) DO 7z.exe a "e:\%~ni.7z" "%i"

In a batch file you'd need:

FOR /D %%i IN (d:\dir*.) DO 7z.exe a "e:\%%~ni.7z" "%%i"

BTW, you can find help on theFORcommand by typing:

help for

at the command prompt.

Note that 7-zip has a separate command-line version called7za.exeyou probably would want to use instead of7z.exe. It's in a separate.zipfile download titled the "7-Zip Command Line Version", which you can find at the 7-Zip download page.

share|improve this answer
    
I assume for different named directories it's IN ( * . * ) instead? I tested it, it works. Just asking to be sure. –  Don Salva Jul 20 '11 at 13:18
    
IN (*.*) would match all the subdirectories of whats is in the current drive+folder. Using something like (<drive_letter>:\*.*) gets all the directories in the root folder of the specified drive. In my example, d:\dir*.* would match the directories whose names literally start with the letters "dir" in the root folder of drive d:. –  martineau Jul 20 '11 at 14:16
    
Gotcha, thanks! –  Don Salva Jul 20 '11 at 15:23
    
BTW, you can also just literally list the folders you want separated by spaces. i.e. IN (d:\some\folder d:\another c:\stuff). –  martineau Jul 20 '11 at 17:30

If you have tons of directories, using wildcard could reach some system limits.

With Cygwin or other Unix tools for Windows as UnxUtils, you could use the 'find'Unix command as follow:

cd <source directory>
find . -mindepth 1 -maxdepth 1 -type d -exec 7za a /<destination directory>/{}.7z {} \;

The '-mindepth'is important to avoid having the current directory returned by 'find'

share|improve this answer
    
I'm trying to use 7zip to its limit, thanks. –  Don Salva Jul 20 '11 at 8:54
    
I do not think that you will find a way to avoid shell for creating many 7z files. For each 7Z file that you want to create, you will need to call 7za. The find command above calls 7z for each directory in <source directory> (exec argument). Maybe someone else will be able to give you the Windows shell equivalent of my Unix shell above. –  jfgagne Jul 20 '11 at 9:00
    
Martineau's method work's just fine. –  Don Salva Jul 20 '11 at 13:18
    
Yes, Martineau's method is the pure Windoze way to do it without touching UNIX utilities. But be careful on the number of directories returned by '(d:\dir*.)': if it is huge, it could break as it would in UNIX (I would like to have the opinion on a Windows shell expert on that please). –  jfgagne Jul 20 '11 at 14:06
    
I'm using Windows 7 and I'm not touching Unix/Linux just for this one matter. Thanks, but no thanks. –  Don Salva Jul 20 '11 at 15:23

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.