3

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

4

2 回答 2

1

I think you need to change a couple things.

  1. Change /A to /AD to get just the directories.
  2. Remove the /S so you will only get the top-level directories in C:\Projects.
  3. Inside your FOR loop, change the "c:\backup\projects.rar" to C:\Backup\%%D.rar"

WARNING: This code is untested.

FOR /F "DELIMS==" %%D in ('DIR C:\projects /AD /B') DO ( 
  "C:\Program Files\WinRAR\WinRAR.EXE" m -r "C:\Backup\%%D.rar" "%%D" 
)
于 2010-06-02T13:33:21.523 回答
1

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

@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命令行上使用的开关的详细信息,请在WinRARRar.txt的程序文件夹中打开文本文件,这是控制台版本的手册,并阅读这些开关的说明。Rar.exe

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

从批处理文件中使用的手册WinRAR.exe可以在WinRAR的帮助中找到选项卡Contents下的项目Command line mode

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

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

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

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

于 2016-04-24T13:49:22.297 回答