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

9,997

Solution 1

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.

Solution 2

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:
9,997

Related videos on Youtube

Grumpy ol' Bear
Author by

Grumpy ol' Bear

A meta-level binary dude embedded to a multimedia proxy world! Now we are all sons of bitches. - Kenneth Tompkins Bainbridge Patria O Muerte! - Ernesto 'Che' Guevara One useless man is a shame, two is a law firm, three or more is a congress. - John Quincy Adams Information is the currency of democracy. - Thomas Jefferson Now git of me goddamned lawn!

Updated on September 18, 2022

Comments

  • Grumpy ol' Bear
    Grumpy ol' Bear over 1 year
    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?

  • jfg956
    jfg956 almost 13 years
    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.
  • Grumpy ol' Bear
    Grumpy ol' Bear almost 13 years
    Martineau's method work's just fine.
  • Grumpy ol' Bear
    Grumpy ol' Bear almost 13 years
    I assume for different named directories it's IN ( * . * ) instead? I tested it, it works. Just asking to be sure.
  • jfg956
    jfg956 almost 13 years
    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).
  • martineau
    martineau almost 13 years
    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
    martineau almost 13 years
    BTW, you can also just literally list the folders you want separated by spaces. i.e. IN (d:\some\folder d:\another c:\stuff).