将多个文件压缩成 bzip2

compressing multiple files into bzip2

所以我为 window 找到了这个不错的批次,它可以通过将任何文件拖放到 bzip2 中来将同一目录中具有相同扩展名的每个文件压缩到 bzip2 中,但我想更进一步并在我将文件夹拖放到其中时做到这一点。它会压缩其中的所有文件,包括子文件夹,直到压缩到最后。显然我猜这与使用 %%d 循环有关,但我无法完全弄明白。

@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-Zipz.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

我与您分享这个由 enteleform 在超级用户

上发布的有用且评论很好的批处理脚本

我刚刚将这个变量 Set archivePath="%%~x.zip" 修改为 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-Zipz.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

你也许可以用一行来做到这一点 :

@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%-Zipz.exe" a -tbzip2 "%%~dpH%%~nH.bz2" "%%H" -mx9 -sdel -w >NUL 2>&1)&"%__AppDir__%timeout.exe" /T 3

如果您希望它分多行以便于阅读:

@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%-Zipz.exe" a -tbzip2 "%%~dpH%%~nH.bz2" "%%H" -mx9 -sdel -w >NUL 2>&1
    "%__AppDir__%timeout.exe" /T 3
)

这些示例只有在将目录拖放到其中或在命令行上以目录作为第一个参数调用时才有效。

@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-Zipz.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

为找到解决方案的朋友 Anya 干杯,所以它与上面的脚本一起工作的方式是制作一个批处理文件将其命名为packall.bat

  • 将它保存在任何地方,因为它会在过程结束时自行删除。

  • 当你想将一堆文件压缩成 bz2 时,你可以复制它并将其放入桌面上任意名称的文件夹中。

  • 确保它的名称没有 spaces 也没有它的子文件夹 因为这可能会混淆批处理并使其压缩您的桌面内容一些原因。

  • 点击批处理,会自动压缩同文件夹及其子文件夹内的所有文件,并自动删除。

视频示例:

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

重要说明出于某种原因,如果子文件夹中存在具有相同扩展名的重复文件名,它们将被删除

不要忘记文件夹及其子文件夹名称不应该有 space

祝你好运!