Search code examples
filebatch-filearchivedirectory7zip

7-Zip files in all folders (multiple archives)


How do I make 7-Zip recursively archive all files in a folder for all parent folders?

Currently I have this:

for /d %%X in (C:\Users\mikejoh\Desktop\Modst\Ziptest\*) do "c:\Program Files\7-Zip\7z.exe" a "%%X\*.7z" "%%X\win\*"

But that only takes the parent folders and zips them in the same location...

I believe I would be able to add something like a DEL "%%X% at the end to make it delete the files. However, in this case it would delete the whole folder :(

Example: I have these folders below:

Enter image description here

These folders contain these folders:

Enter image description here

Where I need to zip all files in the Win folder:

Enter image description here

So for every folder it should make an archive inside the Win folder with all files inside the win folder. If everything goes well, it should delete the archived files.

Would something like this be possible to create in a BAT file?


Solution

  • Let's say the folder Modst\ZipTest on your desktop contains following folders and files:

    • Test1
      • Win
        • File1.txt
        • File2.txt
        • File3.txt
      • xxx.txt
    • Test2
      • Win
        • File1.txt
        • File2.txt
        • File3.txt
      • yyy.txt
    • Test3
      • Win
        • File1.txt
        • File2.txt
        • File3.txt
      • zzz.txt

    And the result should be following list of files and folder with all missing files in a Win subfolder added to the *.zip file in the same subfolder.

    • Test1
      • Win
        • Test1.zip
      • xxx.txt
    • Test2
      • Win
        • Test2.zip
      • yyy.txt
    • Test3
      • Win
        • Test3.zip
      • zzz.txt

    Then this can be achieved with this batch code using WinRAR:

    @echo off
    for /D %%A in ("%USERPROFILE%\Desktop\Modst\ZipTest\*") do (
       "%ProgramFiles%\WinRAR\WinRAR.exe" m -afzip -cfg- -ed -ep -ibck -inul -m5 -tl -u -y "%%~A\Win\%%~nA.zip" "%%~A\Win\*"
    )
    

    The advantage is that WinRAR deletes only files which are successfully added to the archive. All files being locked while WinRAR wants to read file contents for compression remain in the folders. Don't know if this batch file is executed ever while files to compress and delete are written currently in the folders by another application.

    The 7-Zip solution requires more commands in a batch file:

    @echo off
    for /D %%A in ("%USERPROFILE%\Desktop\Modst\ZipTest\*") do (
        "%ProgramFiles%\7-Zip\7z.exe" u -tzip -mx=9 -y "%%~A\%%~nA_tmp.zip" "%%~A\Win\*">nul
        if not errorlevel 1 (
            del /F /Q "%%~A\Win\*"
            move "%%~A\%%~nA_tmp.zip" "%%~A\Win\%%~nA.zip"
        ) else (
            if exist "%%~A\%%~nA_tmp.zip" del "%%~A\%%~nA_tmp.zip"
        )
    )
    

    There is no test made if each file is really added to the archive before it is deleted. No file is deleted if any error occurred during compression of any file in a Win subfolder with exception of the ZIP file in parent folder if the ZIP file was created at all.

    NOTE: The path to WinRAR.exe or 7z.exe can be different on your computer and must be adapted in this case in the batch code.