如何使用WinRAR单独归档目录中的每个文件夹?

时间:2010-06-02 02:22:32

标签: batch-file winrar

我正在尝试使用 WinRAR 来单独压缩所有不同的文件夹。

之前的文件夹内容示例
c:\projects\test
c:\projects\country
c:\projects\db

并在运行批处理文件

之后
c:\backup\test.rar
c:\backup\country.rar
c:\backup\db.rar

我在批处理文件中尝试以下命令。但它会压缩项目文件夹中的所有文件夹进入备份存档:

for /f "delims==" %%D in ('DIR C:\projects /A /B /S') do (
    "C:\Program Files\WinRAR\WinRAR.EXE" m -r "c:\backup\projects.rar" "%%D"
)

c:\backup\projects.rar包含我在单独档案中所需的所有文件。

如何修改批处理文件中的3行以获取所需的存档?

2 个答案:

答案 0 :(得分:1)

我认为你需要改变一些事情。

  1. /A更改为/AD以获取目录。
  2. 删除/S,这样您才能获得C:\Projects中的顶级目录。
  3. FOR圈内,将"c:\backup\projects.rar"更改为C:\Backup\%%D.rar"

  4. 警告:此代码未经测试。

    FOR /F "DELIMS==" %%D in ('DIR C:\projects /AD /B') DO ( 
      "C:\Program Files\WinRAR\WinRAR.EXE" m -r "C:\Backup\%%D.rar" "%%D" 
    )
    

答案 1 :(得分:0)

这是一个批处理文件,用于更常用的此常见任务,因为可以将包含要归档的子文件夹的文件夹指定为运行批处理文件的第一个参数。

@echo off
setlocal
set "BackupFolder=C:\Backup"

rem Folder to archive can be optionally specified as parameter.
if "%~1" == "" (
    set "FolderToArchive=C:\projects"
) else (
    set "FolderToArchive=%~1"
)

rem Check existence of the folder to archive.
if not exist "%FolderToArchive%\*" (
    echo.
    echo Error: Folder %FolderToArchive% does not exist.
    echo.
    endlocal
    pause
    goto :EOF
)

rem Check existence of backup folder and create this folder
rem if not already existing with verification on success.
if not exist "%BackupFolder%\*" (
    md "%BackupFolder%"
    if errorlevel 1 (
        echo.
        echo Error: Folder %BackupFolder% could not be created.
        echo.
        endlocal
        pause
        goto :EOF
    )
)

rem Archive each subfolder in specified or default folder to archive
rem as separate archive with name of folder as archive file name and
rem with current date and an automatically incremented number with at
rem least 2 digits appended to the archive file name to be able to
rem create multiple archives on different days and even on same day.

rem Parent directory path of each subfolder is removed from archive.
rem The name of the subfolder itself is added to each archive. This
rem can be changed by replacing "%%D" with "%%D\" or "%%D\*". Then
rem the files and subfolders of the compressed folder would be added
rem to archive without the name of the compfessed folder.

rem Best compression is used on creating a solid archive with 4 MB
rem dictionary size. All messages are suppressed except error messages.
rem The last modification time of the created archive file is set to
rem date and time of newest file inside the archive.

set "RarError=0"

for /D %%D in ("%FolderToArchive%\*") do (
    echo Archiving %%D ...
    "%ProgramFiles%\WinRAR\Rar.exe" a -ag_YYYY-MM-DD_NN -cfg- -ep1 -idq -m5 -md4m -r -s -tl -y "%BackupFolder%\%%~nD.rar" "%%D"
    if errorlevel 1 set "RarError=1"
)

rem Wait for a key press if an error occurred on creating an archive file.
if "%RarError%" == "1" (
    echo.
    pause
)
endlocal

有关 Rar 命令行中已使用的开关的详细信息,请在 WinRAR 的程序文件文件夹中打开文本文件Rar.txt,该文件夹是控制台版本的手册{ {1}}并阅读这些开关的说明。

注意:命令 a (添加到存档)在上面的批处理代码中使用,而不是 m (移至存档)。

在批处理文件中使用Rar.exe的手册可以在项目命令行模式<项目目录下的 WinRAR 的帮助下找到/ em>的

WinRAR 的控制台和GUI版本之间的切换列表存在一些差异。例如,WinRAR.exe也支持创建WinRAR.exe不支持的ZIP存档。因此Rar.exe支持控制台版本不支持的交换机WinRAR.exe。或者控制台版本的开关-af<type>(安静模式)是用于GUI版本的开关-idq(在后台运行)。

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • -ibck
  • echo /?
  • endlocal /?
  • for /?
  • if /?
  • md /?
  • pause /?
  • rem /?
  • set /?

注意:通过在 WinRAR 中选择要归档的文件夹,点击工具栏中的添加图标,也可以使用 WinRAR 完成此类归档,在存档名称上插入setlocal /?,并在选项卡文件上启用选项将每个文件分别存档。通过开关定义的上述批处理文件中使用的其他选项可在选项卡常规备份时间中找到。