Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: How can I compress multiple depth subfolders?  (Read 75396 times)

0 Members and 1 Guest are viewing this topic.

Retro Jack

    Topic Starter


    Newbie

    • Experience: Experienced
    • OS: Windows 10
    How can I compress multiple depth subfolders?
    « on: January 04, 2022, 12:28:05 AM »
    Can anyone help me with a batch file to compress multiple depth subdirectories?

    I would like to be able to turn

    +---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

    +---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 Piz.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.  :)

    erobby



      Beginner

      • Experience: Experienced
      • OS: Linux variant
      Re: How can I compress multiple depth subfolders?
      « Reply #1 on: December 14, 2022, 01:55:32 PM »
      If you are using 7 Zip "-r-" can be used to copy only current directory without subdirectories

      Hackoo



        Hopeful
      • Thanked: 42
      • Experience: Expert
      • OS: Windows 10
      Re: How can I compress multiple depth subfolders?
      « Reply #2 on: December 16, 2022, 05:58:02 AM »
      Hi  ;)
      May be like this way :
      Code: [Select]
      @echo off
      Title 7z Compressing muti-subfolders
      :: set the current directory to the batch file location
      cd /d %~dp0
      Call :Check_7Zip
      @for /d /r %%D in (*) do (
      PUSHD %%D
      @for %%A in (*.*) do (
      echo "%Zip%" a "%%D.7z" "%%A"
      "%Zip%" a "%%D.7z" "%%A"
      )
      POPD
      )
      pause
      Exit
      ::-----------------------------------------------------------------------------------------------------------------------------
      :Check_7Zip
      Reg Query "HKEY_CURRENT_USER\SOFTWARE\7-Zip" /v "Path">nul 2>&1
      If [%errorlevel%] EQU [1] ( echo 7-Zip is not found !
      ) else (
      @FOR /f "tokens=2*" %%i in ('Reg Query "HKEY_CURRENT_USER\SOFTWARE\7-Zip" /v "Path" 2^>nul') do (Set "Zip=%%j7z.exe")
      )
      Exit /B
      ::-----------------------------------------------------------------------------------------------------------------------------