winrar 命令行:从一个目录创建多个 winrar 密码文件

winrar command line: create multiple winrar passworded files from one directory

我想通过命令行或应用自动压缩单个目录中的多个文件,每个文件都使用相同的密码。

file1 -> file1(w/password).rar

file2 -> file2(w/password).rar

文件 3 -> 文件 2(w/password).rar

Packing (WinRAR) with a password on a group of files 中的答案接近我想要的,但对我的需要来说太复杂了。

我知道可以从命令行轻松完成反向操作。

有什么想法吗?谢谢

这是这个带有额外奖励的简单任务的批处理代码:
工作目录可以作为第一个参数传递给批处理文件。
如果不带参数启动批处理文件,则使用当前目录。

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "WorkingDirectory="
if "%~1" == "" goto ArchiveFiles

set "WorkingDirectory=%~1"
if "%WorkingDirectory:~-1%" == "\" (
    if exist "%WorkingDirectory%*"  pushd "%WorkingDirectory%" & goto ArchiveFiles
) else (
    if exist "%WorkingDirectory%\*" pushd "%WorkingDirectory%" & goto ArchiveFiles
)
echo Directory "%WorkingDirectory%" does not exist.
endlocal
goto :EOF

:ArchiveFiles
for /F "delims=" %%I in ('dir /A-D /B * 2^>nul') do (
    if /I not "%%~xI" == ".rar" (
        "%ProgramFiles%\WinRAR\Rar.exe" a -@ -cfg- -ep1 -idq -m5 -ma4 "-pPassword" -r- -s- -y -- "%%~nI.rar" "%%~fI"
    )
)
if not "%WorkingDirectory%" == "" popd
endlocal

此批处理文件忽略目录中已存在的 *.rar 文件。

WinRAR 的程序文件文件夹中打开文本文件 Rar.txt 以获取有关使用命令的详细信息 a和使用的开关。

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

  • call /? ... 说明 %~1
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • popd /?
  • pushd /?
  • set /?
  • setlocal /?

另请阅读有关 Using Command Redirection Operators 解释 2>nul 的 Microsoft 文章,在此代码中,重定向运算符 > 必须使用脱字符 ^ 进行转义,以便首先解释为解析 FOR 命令行时的文字字符以及 FOR.[= 执行 DIR 时作为重定向运算符30=]

另请阅读 Single line with multiple commands using Windows batch file 上的答案,以了解此处在两个命令行中使用的运算符 & 的含义。