7z - compress files in sub-directory, name them for parent folder

I'm trying to compress a folder structure like so:

Root |_ Folder |_Sub Folder 1 |_Files |_Sub Folder 2 |_Files |_ *more Sub Folders*

I need to create .zip file for each Sub Folder that contains the files within the Sub Folder, and needs to be named for the Sub Folder. So if I run the command within "Folder", I'd get a zip file named for each Sub Folder, with the contents of the Sub Folder only (not the Sub Folder just zipped up).

I've tried various for commands on Windows using 7-zip but can't seem to get it.

for /d %i in (*) do 7z a -oC:\Temp... %i.zip

That just zips the entire Folder contents and loops through creating an archive for each sub-folder name.

I appreciate any help you can provide!

2 Answers

Easiest way is to install TotalCommander (from ghisler's site), select desired subdirs (may use * on numeric keyboard to toggle all). Alt-F5 shortcut to compressors. Check "Create separate archives, one per selected file/dir", select desired compressor and OK.

In a Batch you can use:

@echo off
for /d %%i in (*) do ( echo Compressing %%i 7z a "C:\TEMP\%%i.7z" -r "%%i\*.*"
)

Or in a CMD line:

for /d %i in (*) do 7z a "C:\TEMP\%i.7z" -r "%i\*.*"
2

You can also use Powershell :

Get-ChildItem -Path "C:\Root\Folder" -Force -Directory -ErrorAction Continue | foreach {Start-Process "C:\Program Files\7-Zip\7z.exe" -ArgumentList "a C:\TEMP\$_.7z -r $($_.FullName)\*.*"}
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like