我对批处理文件编码非常陌生,需要你的帮助。
我有这些目录:
c:\rar\temp1\xy.jpg
c:\rar\temp1\sd.jpg
c:\rar\temp1\dd.jpg
c:\rar\temp2\ss.jpg
c:\rar\temp2\aa.jpg
c:\rar\temp2\sd.jpg
c:\rar\temp3\pp.jpg
c:\rar\temp3\ll.jpg
c:\rar\temp3\kk.jpg
我想把它们压缩成这样
c:\rar\temp1\temp1.rar
c:\rar\temp2\temp2.rar
c:\rar\temp3\temp3.rar
如何使用 WinRAR 完成此操作?
这也可以使用 WinRAR 完成,无需使用批处理文件,不完全按照要求,但与想要的类似。
c:\rar\
。temp1
、temp2
和 temp3
,然后单击工具栏中的按钮 添加。c:\rar\
。WinRAR 现在会在文件夹
temp1.rar
中创建三个文件名为 temp2.rar
、temp3.rar
和 c:\rar\
的 RAR 存档,每个存档包含相应的文件夹以及所有文件和子文件夹。
要添加的文件列表也可以在选项卡文件上进行更改,例如在
要排除的文件中输入
*.txt
,以在创建档案时忽略三个文件夹中的文本文件。
最后,在
要存储但不压缩的文件下的编辑字段中的选项卡文件上输入
*.jpg
是有意义的,因为JPEG文件通常包含已经压缩的数据,因此WinRAR无法真正进一步压缩文件的数据。
这也是一个批处理文件解决方案,用于将c:\rar\
的所有非隐藏子文件夹及其子文件夹中的文件
移动到存档文件中,并根据要求在每个子文件夹中创建子文件夹的名称。
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "RAREXE=Rar.exe"
if exist "%RAREXE%" goto CreateArchives
if exist "%ProgramFiles%\WinRAR\Rar.exe" set "RAREXE=%ProgramFiles%\WinRAR\Rar.exe" & goto CreateArchives
if exist "%ProgramFiles(x86)%\WinRAR\Rar.exe" set "RAREXE=%ProgramFiles(x86)%\WinRAR\Rar.exe" & goto CreateArchives
for /F "skip=2 tokens=1,2*" %%I in ('%SystemRoot%\System32\reg.exe query "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe" /v Path 2^>nul') do (
if /I "%%I" == "Path" if exist "%%~K\Rar.exe" for %%L in ("%%~K\Rar.exe") do set "RAREXE=%%~fL" & goto CreateArchives
)
for /F "skip=2 tokens=1,2*" %%I in ('%SystemRoot%\System32\reg.exe query "HKCU\Software\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe" /v Path 2^>nul') do (
if /I "%%I" == "Path" if exist "%%~K\Rar.exe" for %%L in ("%%~K\Rar.exe") do set "RAREXE=%%~fL" & goto CreateArchives
)
for /F "delims=" %%I in ('%SystemRoot%\System32\where.exe Rar.exe 2^>nul') do set "RAREXE=%%I" & goto CreateArchives
echo ERROR: Could not find Rar.exe!
echo/
echo Please define the variable RAREXE at top of the batch file
echo "%~f0"
echo with the full qualified file name of the executable Rar.exe.
echo/
pause
goto :EOF
:CreateArchives
set "Error="
for /D %%I in ("c:\rar\*") do (
echo Creating RAR archive for "%%I" ...
"%RAREXE%" m -@ -cfg- -ep1 -idq -m3 -msgif;png;jpg;rar;zip -r -s- -tl -y -- "%%I\%%~nxI.rar" "%%I\"
if errorlevel 1 set "Error=1"
)
if defined Error echo/& pause
endlocal
使用正确的完整限定文件名定义环境变量
set "RAREXE=Rar.exe"
时,可以省略 :CreateArchives
之后到 RAREXE
之间的行。
请阅读
WinRAR程序文件文件夹中的文本文件
Rar.txt
,了解 RAR 命令 m
和所用开关的说明。该问题根本不包含应使用哪些选项创建 RAR 存档的任何信息。
要了解所使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完整、仔细地阅读每个命令显示的帮助页面。
call /?
... 解释 %~f0
... 批处理文件的全名echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
reg /?
reg query /?
set /?
setlocal /?
where /?
另请参阅 使用 Windows 批处理文件的单行多个命令,了解运算符的说明
&
。
阅读有关 使用命令重定向运算符的 Microsoft 文档,了解
2>nul
的说明。重定向运算符 >
必须在三个 FOR命令行上使用脱字符号
^
进行转义,以便在 Windows 命令解释器在执行命令 FOR(执行嵌入的 reg
)之前处理此命令行时将其解释为文字字符或 where
命令行,使用在后台启动的单独命令进程。
这个脚本也可以工作:
@echo off
for %%a in ("C:\rar\temp1" "C:\rar\temp2" "C:\rar\temp3") do (
pushd "%%~a"
"C:\Program Files\WinRAR\rar.exe" a -r temp.rar *
popd
)
在 Python v3.x 中:
import os
# NOTE: Script is disabled by default, uncomment final line to run for real.
base_dir = "E:\target_dir"
# base_dir = os.getcwd() # Uncomment this to run on the directory the script is in.
# Stage 1: Get list of directories to compress. Top level only.
sub_dirs_raw = [os.path.join(base_dir, o) for o in os.listdir(base_dir) if os.path.isdir(os.path.join(base_dir, o))]
# Stage 2: Allow us exclude directories we do not want (can omit this entire section if we wish).
dirs = []
for d in sub_dirs_raw:
if "legacy" in d or "legacy_old" in d:
continue # Skip unwanted directories
print(d)
dirs.append(d)
# Stage 3: Compress directories into .rar files.
for d in dirs:
os.chdir(d) # Change to target directory.
# Also adds 3% recovery record using "-rr3" switch.
cmd = f"\"C:\Program Files\\WinRAR\\rar.exe\" a -rr3 -r {d}.rar *"
print(cmd)
# Script is disabled by default, uncomment this next line to execute the command.
# os.system(cmd)
备注:
os.system(cmd)
取消最后一行 #
的注释。mydir1
、mydir2
、mydir3
,则会创建三个 .rar
文件:mydir1.rar
、mydir2.rar
、mydir3.rar
。script.py
中,然后从任何目录运行DOS命令python script.py
。使用第二行设置目标目录。或者,使用 PyCharm 运行脚本。这应该可以工作,它还可以检查文件是否被正确压缩。 您可能需要根据 winrar 的安装位置更改这部分“cd Program Files\WinRAR”。
@echo Off
Cd\
cd Program Files\WinRAR
rar a -r c:\rar\temp1\temp1.rar c:\rar\temp1\*.jpg c:\rar\temp1\
if "%ERRORLEVEL%"=="0" ( Echo Files compressed
) Else Echo Failed
rar a -r c:\rar\temp2\temp2.rar c:\rar\temp2\*.jpg c:\rar\temp2\
if "%ERRORLEVEL%"=="0" ( Echo Files compressed
) Else Echo Failed
rar a -r c:\rar\temp3\temp3.rar c:\rar\temp3\*.jpg c:\rar\temp3\
if "%ERRORLEVEL%"=="0" ( Echo Files compressed
) Else Echo Failed
Pause