@echo off
::变量初始化
SETLOCAL ENABLEDELAYEDEXPANSION
set logpath=comblog.txt
set input1count=0
set input2count=0
set i1=0
set i2=0
set size=0
color 3f
if not exist output md output
::goto :readresult
echo 注意:路径中有空格要加引号
:inipath1
set /p input1path=输入含jpg、png的目录路径(若输入0,则使用本目录下的input1目录):
if %input1path%==0 (set input1path=input1) else (
if not exist %input1path% echo 该目录不存在,请重新输入&&goto :inipath1 )
:inipath2
set /p input2path=输入含有rar的目录路径(若输入0,则使用本目录下的input2目录):
if %input2path%==0 (set input2path=input2) else (
if not exist %input2path% echo 该目录不存在,请重新输入&&goto :inipath2 )
::统计input1path中图片文件个数,和input2path中rar数量(当然也可以把结果存到临时文件中,之后直接for /f调用二重循环调用)
for /r %input1path% %%a in (*.jpg,*.png) do (
set /a input1count+=1 )
for /r %input2path% %%a in (*.rar) do (
set /a input2count+=1 )
echo ***共有%input1count%个图片文件,%input2count%个rar文件
::二重循环遍历文件,也可以用 for /f 结合dir,只是用dir比较卡顿
for /r %input1path% %%a in (*.jpg,*.png) do (
::echo %%a
set /a i1+=1
set i2=0
::图的数量<=rar文件的数量,若文件数量多则用最后一张图连接剩下的rar
for /r %input2path% %%b in (*.rar) do (
set /a i2+=1
if !i1! == !i2! call :combine "%%a" "%%b"
if !i2! gtr !i1! if !i1! geq !input1count! call :combine "%%a" "%%b"
if !i1!==%input2count% if !i2! geq %input2count% goto :end))
echo ========================================================================
:end
echo %time% 操作完成,共合并了(%i1% X %i2%)个文件,%size%bytes
echo 具体细节保存在了%logpath%
echo ^:%date%_%time%,%i1,%i2 >> %logpath%
pause
::goto :readresult
goto :eof
:combine
copy /b %1+%2 output\"%~n1_%~n2%~x1"
echo %1^|%2^|"%~n1_%~n2%~x1" >> %logpath%
::文件名中没有|符号
set /a tsize=%~z1
set /a tsize+=%~z2
set /a size+=%tsize%
echo %tsize% bytes
::后面输出参数也要加引号,因为字符串扩展会自动去除引
goto :eof
::暂时此函数无用
:readresult
for /f "eol=: tokens=1-3 delims=|" %%a in (%logpath%) do (
echo %%a,%%b,%%c)
pause