将多个文件压缩到多个档案中?

将多个文件压缩到多个档案中?

我有一个装满 pdf 的文件夹,我厌倦了单独对每个文件进行 Winrar。我不想将它们全部放在一个存档中。

是否没有命令行可以用来让 Winrar 以最佳方法压缩文件,在制作 .rar 后删除该文件,然后对下一个文件执行相同操作?

答案1

是的,WinRAR 安装文件夹中有一个可执行文件 rar.exe。运行它而不带参数,您将获得有关其用法的完整帮助。例如,您可以使用 -m5 开关设置最佳压缩级别。

正如所说,批处理/shell 脚本是一种很好的方法,但你可能会发现这个具有类似目标的项目很有趣:http://code.google.com/p/autorar/

答案2

这是我编写的脚本,用于使用 rar 归档程序(winrar5 格式)分别压缩每个文件。要分别压缩文件夹中的所有 *.pdf 文件,您必须将此脚本放入 .bat 文件中,然后运行它。首先,指定文件所在的起始(根)目录,然后可以指定要在该目录中查找和压缩的扩展名(递归搜索,进入根目录的子目录)

@echo off

REM -m5 max compression    
REM -md64m dictionary size 64m
REM -s solid
REM -ma5 rar5 format (rar4 - ma4)
REM -y yes to all
REM -t test after packing (if test NOT ok, then files will not be deleted if the -df/-dr specified)
REM -df delete files after successful archivation
REM -ep1 exclude base folder of file from archive
:: %%~nZ - file name extracted from var Z
:: %%~fZ - full path of file+ext from var Z
:: %%~dpZ - disk+path
:: setlocal - use local environment vars
setlocal

:step1
    echo Enter start path where files a located
    set /p rarpath=
    echo.
    :: remove doublequotes from path
    set rarpath=%rarpath:"=%

:check dir exist
    if not exist "%rarpath%" echo   "!!!  [%rarpath%] doesn't exist, please enter correct path  !!!"
    if not exist "%rarpath%" echo.
    if not exist "%rarpath%" goto step1
:::::
:::::
    echo.
    echo Enter which extensions to compress (like *.mp4;*.wmv)
    set /p exten=
    echo.
:::::
:::::
    echo.
    echo Final command: 
    echo for /R "%rarpath%" %%Z in (%exten%) do "%programfiles%\WinRAR\rar.exe" a -ma5 -m5 -s -md64m -t -y -ep1 -df "%%~nZ.rar" "%%~fZ"
    echo.
:::::
:::::
    echo.
    echo Continue? [y/n]
    set /p go=
    if /i %go%==Y goto ok
    if /i %go%==N goto exit

:ok
:: CD to each file's directory, compress file, log output (codepage 866)
set rartime=%time:~0,8%
set rartime=%rartime::=_%
set rartime=%rartime: =%
set rarlog=rar_log_%date%_%rartime%.txt
:: For directories located in current directory
:: for /d %D in (*) do "%programfiles%\winrar\rar.exe" a -ma5 -m5 -s -md64m -t -y -ep1 -df "%D.rar" "%D"

    for /R "%rarpath%" %%Z in (%exten%) do (
        cd /d %%~dpZ
        "%programfiles%\WinRAR\rar.exe" a -ma5 -m5 -s -md64m -t -y -ep1 -df "%%~nZ.rar" "%%~fZ" >> c:\%rarlog% 2>&1
        type c:\%rarlog%
        echo. >> c:\%rarlog%
        echo ----------------------------------------------------------------------------- >> c:\%rarlog%
        echo. >> c:\%rarlog%
    )
    echo.
    echo.
    echo        !!! All Done !!!
    pause
    goto :EOF
    ::EXIT /B 0

:exit
    echo Program terminated
    pause
    goto :EOF

相关内容