0

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
  • Hello and welcome to StackOverflow! Is your code not working as intended? – Daniel Walker Jul 06 '20 at 02:58
  • it works, but what it does currently is when you drag and drop a file into it. it will compress all of the other files of the same extension into bz2. for example lets say the folder Stories has story1.txt story2.txt story3.txt and a subfolder called Novels that has Novel1.txt Novel2.txt now i want to compress them into bz2, so i take my batch and put it in the folder Stories all i have to do is drag and drop any of the txt files and it will compress them all automatically into bz2 but only the files in Stories not the subfolder Novels as i would have to move my batch there – Emotiworld Jul 06 '20 at 03:05
  • in Novels and do the same for the txt files. but that would be tiresome when i have more subfolders and have to do it multiple times. so i wish to make my batch work by folder instead of file. so when i drag and drop the folder into it. it will compress all of its contents (files) into bz2 with their subfolders automatically so with one drag and drop story1.txt story2.txt story3.txt in Stories will be compressed and so will Novel1.txt Novel2.txt in the subfolder Novels and stay in their directories respectively of course – Emotiworld Jul 06 '20 at 03:08

3 Answers3

0

I share with you this helpful and well commented batch script posted by enteleform on superuser

I just modified this variable Set archivePath="%%~x.zip" to Set archivePath="%%~x.bz2"

How to make 7-zip do a whole bunch of folders

@Echo OFF
SetLocal EnableDelayedExpansion
Rem //  7-Zip Executable Path
Set sevenZip="C:\Program Files\7-Zip\7z.exe"
Rem // START: NewLine Variable Hack
Set newLine=^


Rem // END: NewLine Variable Hack !! DO NOT DELETE 2 EMPTY LINES ABOVE !!
Rem //  Set ErrorLog Variables
Set errorCount=0
Set separator=--------------------------------------------------------
Set errorLog=!newLine!!newLine!!separator!!newLine!!newLine!
Set errorPrefix=ERROR @:
Set successMessage=All Files Were Successfully Archived

Rem //  Loop Through Each Argument
SetLocal DisableDelayedExpansion
for %%x in (%*) do (

    Rem //  Use Current Argument To set File, Folder, & Archive Paths
    SetLocal DisableDelayedExpansion
    Set filePath="%%~x"
    Set directoryFiles="%%~x\*"
    Set archivePath="%%~x.bz2"
    SetLocal EnableDelayedExpansion

    Rem //  Source Is A Folder
    if exist !directoryFiles! (
            Set sourcePath=!directoryFiles!
    )

    Rem //  Source Is A File
    if not exist !directoryFiles! (
            Set sourcePath=!filePath!
    )

    Rem //  Print Separator To Divide 7-Zip Output
    echo !newLine!!newLine!!separator!!newLine!!newLine!

    Rem //  Add Files To Zip Archive
    !sevenZip! A -TZIP !archivePath! !sourcePath!

    Rem //  Log Errors
    if ErrorLevel 1 (
        Set /A errorCount=errorCount+1
        Set errorLog=!errorLog!!newLine!!errorPrefix!!sourcePath!
    )
)

Rem //  Print ErrorLog
if !errorCount!==0 (
    Set errorLog=!errorLog!!newLine!!successMessage!
)
Echo !errorLog!!newLine!!newLine!!newLine!
Rem //  Keep Window Open To View ErrorLog
pause
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • thank you but from my understanding it seems like what this does is basically compress the whole thing into one bz2 which is not what i am looking for. if it were i could just use **add to archive** option from 7-zip that would do just the same. what i am looking for is that it compresses every file individually inside of the folder and its subfolders. i will show you what the batch i posted does http://billstuff.site.nfoservers.com/u57c783r.mp4 look how it compresses files individually it is amazing but it would only do that to one extension, if i wanted another i would have to – Emotiworld Jul 06 '20 at 11:14
  • repeat again, which is a problem in my end because i have bunch of stuff that i have to compress that keep coming and and the process is becoming tiresome so im looking for a way where i just drag and drop the folder containing everything then it just goes through every file individually compressing it then accessing its subfolders if any exist and compress files there individually until the end however i appreciated your input, thank you very much Hackoo – Emotiworld Jul 06 '20 at 11:17
0

You could probably do that with a single line :

@For %%G In ("%~1")Do @If "%%~aG" GEq "d" (For /F Delims^= %%H In ('""%__AppDir__%where.exe" /R "%%~G" * 2>NUL|"%__AppDir__%findstr.exe" /EVIL ".bz2""')Do @"%ProgramFiles%\7-Zip\7z.exe" a -tbzip2 "%%~dpH%%~nH.bz2" "%%H" -mx9 -sdel -w >NUL 2>&1)&"%__AppDir__%timeout.exe" /T 3

If you'd like it over multiple lines for readability:

@For %%G In ("%~1") Do @If "%%~aG" GEq "d" (
    For /F "Delims=" %%H In (
        '""%__AppDir__%where.exe" /R "%%~G" * 2>NUL | "%__AppDir__%findstr.exe" /EVIL ".bz2""'
    ) Do @"%ProgramFiles%\7-Zip\7z.exe" a -tbzip2 "%%~dpH%%~nH.bz2" "%%H" -mx9 -sdel -w >NUL 2>&1
    "%__AppDir__%timeout.exe" /T 3
)

These examples should only work if you drag and drop a directory onto it, or call it on the command line with a directory as the first argument.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • Thank you very much, this is simpler than my solution but could you please make it print the progress into the cmd so people know how long to wait? – Emotiworld Jul 07 '20 at 21:57
  • Change `>NUL 2>&1` to `2>NUL`, @Emotiworld. – Compo Jul 07 '20 at 22:51
  • @Emotiworld, just for information, I used the `-w` option to use the default Windows temporary location as the working directory. If your files are not on the system drive, then I'd advise that you remove the `-w`, _(to work directly in the source directory)_, or set it to another location on the same volume ,e.g. `-wF:\Temp` – Compo Jul 07 '20 at 23:54
  • one last thing, I noticed that it does not remove some files after compression https://i.imgur.com/RxvlyBO.png – Emotiworld Jul 09 '20 at 10:15
  • also it does not compress all kind of files – Emotiworld Jul 09 '20 at 10:38
0
@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!