How can I compress multiple depth subfolders?


  1. Posts : 3
    Windows 10 Pro 21H1
       #1

    How can I compress multiple depth subfolders?


    Can anyone help me with a batch file to compress multiple depth subdirectories?

    I would like to be able to turn

    Code:
    +---Pix
    |   |   a.jpg
    |   |   b.jpg
    |   |   c.jpg
    |   |   
    |   \---Docs
    |       |   a.doc
    |       |   b.doc
    |       |   c.doc
    |       |   
    |       \---Bills
    |               a.xls
    |               b.xls
    |               c.xls
    |               
    \---Wishlist
        |   x.jpg
        |   y.jpg
        |   z.jpg
        |   
        \---Adverts
            |   x.doc
            |   y.doc
            |   z.doc
            |   
            \---Income
                    x.xls
                    y.xls
                    z.xls
    into
    Code:
    +---Pix
    |   |   Pix.7z
    |   |   
    |   \---Docs
    |       |   Docs.7z
    |       |   
    |       \---Bills
    |               Bills.7z
    |               
    \---Wishlist
        |   Wishlist.7z
        |   
        \---Adverts
            |   Adverts.7z
            |   
            \---Income
                    Income.7z
    Notes:
    • My archiver (7z.exe) is already set in my path environment variable, so the batch file doesn't need to know where it is.
    • The contents of Pix.7z (for example) should only include the files in the Pix folder, not the contents of Docs or Bills.



    Any help would be greatly appreciated.
      My Computer


  2. Posts : 42,991
    Win 10 Pro (22H2) (2nd PC is 22H2)
       #2

    Hi, it looks as if this has been addressed here:
    batch - How to make 7-zip do a whole bunch of folders - Super User

    But you may need to be prepared to test whatever you decide to try carefully- and when you do try this, use a copy of your original files just to be even safer. (I.e. always keep a backup).
      My Computers


  3. Posts : 3
    Windows 10 Pro 21H1
    Thread Starter
       #3

    dalchina said:
    Hi, it looks as if this has been addressed here:
    batch - How to make 7-zip do a whole bunch of folders - Super User

    But you may need to be prepared to test whatever you decide to try carefully- and when you do try this, use a copy of your original files just to be even safer. (I.e. always keep a backup).
    I've just tried that, but unfortunately, that just zips each top-level folder and its contents into its own file; what I'm after would put each subfolder into their own zip files within the tree.

    Thanks anyway.
      My Computer


  4. Posts : 42,991
    Win 10 Pro (22H2) (2nd PC is 22H2)
       #4

    I understand what you're looking for - there are variants on the idea in the comments following. I can't be sure if any of them, from the limited description, achieve exactly that, so I'd have to leave that to you. E.g. versions around this comment
    7-zip has an option to UNZIP every file into its own folder (*/), but lacks the context commands to do the opposite action you are looking for.
    That said, I remember doing this exact thing using winrar in the past. There's an option "unique archive per folder" or something to that effect.
    There are other similar discussions e.g.
    how to zip all files individually in all subfolders and remove original file after - Stack Overflow
    https://social.technet.microsoft.com...ies?forum=ITCG

    The difficulty with any of these is how robust they are.. how they handle exceptions, and how reliable they are.
      My Computers


  5. Posts : 456
    Windows 10
       #5

    So would I be right in the assumption that you want to preserve the folders and only zip their contents into a 7z file with the same name as its folder?

    Would this be as expected:



    Assuming 7z.exe path is already in the %path% variable....
    Code:
    @echo off
    
    :: Please drag and drop the main folder to this batch file
    
    IF /i exist "%~1" (IF /i not exist "%~1\" exit) else (exit)
    pushd "%~1"
    
    for /f "Delims=" %%a in ('dir /b /s /ad') do (
                                                  cd /d "%%a"
                                                  7z.exe a -sdel "%%~na.7z" *.*
                                                 )
    popd
    Last edited by ricardobohner; 07 Jan 2022 at 08:51.
      My Computer


  6. Posts : 3
    Windows 10 Pro 21H1
    Thread Starter
       #6

    ricardobohner said:
    So would I be right in the assumption that you want to preserve the folders and only zip their contents into a 7z file with the same name as its folder?

    Would this be as expected:



    Assuming 7z.exe path is already in the %path% variable....
    Code:
    @echo off
    
    :: Please drag and drop the main folder to this batch file
    
    IF /i exist "%~1" (IF /i not exist "%~1\" exit) else (exit)
    pushd "%~1"
    
    for /f "Delims=" %%a in ('dir /b /s /ad') do (
                                                  cd /d "%%a"
                                                  7z.exe a -sdel "%%~na.7z" *.*
                                                 )
    popd
    Thank you! That is exactly what I was after!

    Muito apreciado!
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 19:37.
Find Us




Windows 10 Forums