Search code examples
windowsbatch-filezipcompressionbzip2

compressing multiple files into bzip2


so i found this nice batch for window that would compress every file of the same extension in the same directory into bzip2 by dragging and dropping any of the files into it but i would like to take it further and make it that when i drag and drop a folder into it. it would compress all the files in it, including the sub-folders until it reaches the end. obviously i would guess it has something to do with looping with using %%d but i could not exactly figure it out.

@echo off
if [%1]==[] goto usage

for /f %%i in ("%1") do (
    echo %%~di
    echo %%~pi
    echo %%~xi
    set rootpath="%%~di%%~pi*%%~xi"
)

for %%f in (%rootpath%) do (
    "C:\Program Files\7-Zip\7z.exe" a -tbzip2 "%%f.bz2" "%%f" -mx9
    del "%%f" /s /f /q
)

echo Finished operations!
goto exit

:usage
echo You have to drag and drop a file on this batch script!
echo Sorry for the poor documentation, but if you'll want to use it, you have to edit the .bat file
echo The only thing you really need is to change the path to your 7-Zip installation
echo Then simply drag and drop a file in a folder you want to BZip2, and it'll do the rest automatically

:exit
pause

Solution

  • @echo off
    
     
    for /f %%i in ("%1") do (
        echo %%~di
        echo %%~pi
        echo %%~xi
        set rootpath="%%~di%%~pi*%%~xi"
    )
     
    for /R %%f in (*) do (
        "C:\Program Files\7-Zip\7z.exe" a -tbzip2 "%%f.bz2" "%%f" -mx9 -x!"packall.bat"
        del "%%f" /s /f /q
    )
    
    echo Finished operations!
    goto exit
    
    :usage
    echo You have to drag and drop a file on this batch script!
    echo Sorry for the poor documentation, but if you'll want to use it, you have to edit the .bat file
    echo The only thing you really need is to change the path to your 7-Zip installation
    echo Then simply drag and drop a file in a folder you want to BZip2, and it'll do the rest automatically
     
    :exit
    pause
    

    Cheers to my friend Anya who found a solution, so the way it would work with the script above is that you make a batch file name it packall.bat

    • save it anywhere as it will delete itself at the end of the process.

    • when you want to compress bunch of files into bz2 you copy it and put it in inside a folder made with any name in your desktop.

    • make sure its name has no spaces nor its sub-folders as that may confuse batch and make it compress your desktop contents for some reason.

    • click on the batch, then it will compress all the files within the same folder its in and their sub-folders and automatically delete itself.

    Video example:

    http://billstuff.site.nfoservers.com/e79nwk69.mp4

    IMPORTANT NOTE for some reason if there duplicate names of files with the same extension at sub-folders they will be deleted

    Don't forget the folder and its sub-folder names should not have a space

    Best of luck!