【问题标题】:How to archive each folder in a directory separately using WinRAR?如何使用 WinRAR 分别归档目录中的每个文件夹?
【发布时间】:2011-02-26 15:08:44
【问题描述】:

我正在尝试使用 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 行以获得所需的存档?

【问题讨论】:

    标签: batch-file winrar


    【解决方案1】:

    我认为你需要改变几件事。

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

    警告:此代码未经测试。

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

    【讨论】:

    • 嗨,感谢您的回复,我试过 FOR /F "DELIMS==" %%D in ('DIR C:\projects /AD /B') DO ("C:\Program Files \WinRAR\WinRAR.EXE" m -r "C:\Backup\%%D.rar" "%%D" ) 但它不起作用。
    • 嗯...你收到消息了吗?
    • 不,它只是运行,出现黑屏,但文件保留在 c:\projects 中,而 c:\backup 中没有任何内容:(
    • 以防万一这些是文件夹。 c:\projects\test\ c:\projects\country\ c:\projects\db\
    • 您是从资源管理器运行它吗?打开命令提示符并从那里运行它,这样脚本完成后窗口仍然会打开。
    【解决方案2】:

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

    @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,这是控制台版本Rar.exe的手册并阅读这些开关的说明。

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

    在批处理文件中使用WinRAR.exe 的手册可以在WinRAR 选项卡Contents 下的命令行模式 项的帮助中找到>.

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

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

    • echo /?
    • endlocal /?
    • for /?
    • if /?
    • md /?
    • pause /?
    • rem /?
    • set /?
    • setlocal /?

    注意:这样的归档也可以使用 WinRAR 完成,方法是在 WinRAR 中选择要归档的文件夹,单击工具栏中的 添加 图标,在 存档名称 上插入 C:\Backup\ 并在 文件 选项卡上启用选项将每个文件放入单独的存档。上面通过开关定义的批处理文件中使用的其他选项可以在标签 GeneralBackupTime 上找到。

    【讨论】:

      猜你喜欢
      • 2021-07-24
      • 2014-09-15
      • 1970-01-01
      • 2013-10-18
      • 2021-08-02
      • 1970-01-01
      • 2016-09-24
      • 2019-06-10
      • 2022-11-15
      相关资源
      最近更新 更多