1
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?

Grumpy ol' Bear
  • 6,271
  • 12
  • 58
  • 78

2 Answers2

3

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 get help on the FOR command by typing:

help for

at the command prompt.

Note that 7-zip has a separate command-line version called 7za.exe you would probably want to use instead of 7z.exe. It's in a separate .7z file download titled the "7-Zip Extra: standalone console version", which you can find at the 7-Zip download page. The archive contains multiple files, two of which are a small console za.exe file and a separate 7za.dll library file which the former uses to do the heavy-lifting — which are what you need.

martineau
  • 4,447
  • 23
  • 29
  • I assume for different named directories it's IN ( * . * ) instead? I tested it, it works. Just asking to be sure. – Grumpy ol' Bear Jul 20 '11 at 13:18
  • `IN (*.*)` would match all the subdirectories of whats is in the current drive+folder. Using something like `(:\*.*)` 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
  • 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
1

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'

jfg956
  • 1,159
  • 8
  • 8
  • 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 (exec argument). Maybe someone else will be able to give you the Windows shell equivalent of my Unix shell above. – jfg956 Jul 20 '11 at 09:00
  • Martineau's method work's just fine. – Grumpy ol' Bear 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). – jfg956 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. – Grumpy ol' Bear Jul 20 '11 at 15:23