0

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 2

0

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
  • This worked great, thank you! Commented Jan 5, 2022 at 20:35
  • Thanks, -r is necesary to recurse subDirs, and -o (output dir option) won't work because I think it's only a decompressing option. Please add quotes (forgotten last set) which are necessary if some DIRs have spaces in their name. Also try TotalCommander which will ease that and other common tasks.
    – isidroco
    Commented Jan 5, 2022 at 21:15
0

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
  • Thank you! I tried this in PowerShell but it end up creating empty zip files for each sub-directory. Thank you though! Commented Jan 5, 2022 at 20:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .