Windows 命令行不支持文件/文件夹路径中的文件夹的通配符。 *\* 因此无效。
需要两个嵌套的 FOR 循环,外部循环在当前目录中运行,处理目录Files1 和Files2,内部循环处理目录files11,files12,@987654326 @、files22 并在这些子目录上运行 Rar。
第一批代码创建存档文件
Files1\files11.rar
Files1\files12.rar
Files2\files21.rar
Files2\files22.rar
每个存档中都包含存档目录files11、files12、files21、files22。
@echo off
set "CompressionError=0"
rem Set directory of batch file as working directory.
pushd "%~dp0"
rem For each directory in working directory run a second loop which
rem compresses each subdirectory of current directory to a RAR archive
rem with deleting all files and subdirectories as well as the archived
rem directory itself and name the archive file like the archived directory.
for /D %%D in (*) do (
for /D %%F in ("%%D\*") do (
"%ProgramFiles%\WinRAR\Rar.exe" a -cfg- -df -ep1 -idq -m5 -md4m -r -s -y "%%F.rar" "%%F"
if errorlevel 1 set "CompressionError=1"
)
)
rem Restore the previous working directory.
popd
rem Halt batch processing if any error occurred on any compression.
if "%CompressionError%" == "1" pause
set "CompressionError="
第二个批处理代码也创建了 4 个存档文件,但存档目录 files11、files12、files21、files22 未添加到存档中。
@echo off
set "CompressionError=0"
rem Set directory of batch file as working directory.
pushd "%~dp0"
rem For each directory in working directory run a second loop which
rem compresses each subdirectory of current directory to a RAR archive
rem with deleting all files and subdirectories as well as the archived
rem directory itself and name the archive file like the archived directory.
for /D %%D in (*) do (
for /D %%F in ("%%D\*") do (
"%ProgramFiles%\WinRAR\Rar.exe" a -cfg- -df -ep1 -idq -m5 -md4m -r -s -y "%%F.rar" "%%F\"
if errorlevel 1 (
set "CompressionError=1"
) else (
rd "%%F"
)
)
)
rem Restore the previous working directory.
popd
rem Halt batch processing if any error occurred on any compression.
if "%CompressionError%" == "1" pause
set "CompressionError="
查看WinRAR 程序文件夹中的文本文件Rar.txt 了解使用Rar 开关的详细信息,这是WinRAR 控制台版本的手册 em>。
两个批处理文件在执行过程中不显示任何内容,如果没有出现错误,则会自动退出。但是如果发生任何压缩错误,批处理会在最后停止,以便批处理用户可以查看Rar输出到控制台窗口的错误消息。
要了解所使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读每个命令显示的所有帮助页面。
-
call /? 解释 %~dp0(参数 0 的驱动器和路径 - 批处理文件)
echo /?
for /?
if /?
pause /?
popd /?
pushd /?
rd /?
rem /?
set /?