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

时间:2017-03-07 20:13:41

标签: batch-file passwords winrar

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

file1 - >文件1(W /密码).rar程序

file2 - >文件2(W /密码).rar程序

file3 - >文件2(W /密码).rar程序

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

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

有什么想法?感谢。

1 个答案:

答案 0 :(得分:0)

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

@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和已使用的开关。

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

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

另请阅读Microsoft Using Command Redirection Operators解释2>nul的文章,其中在此代码中,重定向运算符>必须使用插入符^进行转义,才能首先解释为文字字符解析 FOR 命令行,并作为重定向运算符执行 DIR FOR

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