How do I create seperate 7z files from each selected directory with 7zip command line?

1

3

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

Posted 2011-07-20T07:54:27.830

Reputation: 5 313

Answers

2

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.

martineau

Posted 2011-07-20T07:54:27.830

Reputation: 3 849

I assume for different named directories it's IN ( * . * ) instead? I tested it, it works. Just asking to be sure. – Grumpy ol' Bear – 2011-07-20T13:18:47.033

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 – 2011-07-20T14:16:17.767

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 – 2011-07-20T17:30:23.467

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

Posted 2011-07-20T07:54:27.830

Reputation: 1 021

I'm trying to use 7zip to its limit, thanks. – Grumpy ol' Bear – 2011-07-20T08:54:33.060

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. – jfg956 – 2011-07-20T09:00:42.273

Martineau's method work's just fine. – Grumpy ol' Bear – 2011-07-20T13:18:06.410

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 – 2011-07-20T14:06:17.590

I'm using Windows 7 and I'm not touching Unix/Linux just for this one matter. Thanks, but no thanks. – Grumpy ol' Bear – 2011-07-20T15:23:00.877