首先建立一个批处理文件,命名为comparesize.bat.
@echo off
rem 比较两个文件的大小,返回较大文件的编号,相同返回0.
set file1=%1
set file2=%2
dir /-c %file1%>comparetemp1.txt
dir /-c %file2%>comparetemp2.txt
findstr /c:"1 个文件" comparetemp1.txt>sizetemp1.txt
findstr /c:"1 个文件" comparetemp2.txt>sizetemp2.txt
setlocal EnableDelayedExpansion
for /f "tokens=3 delims= " %%i in (sizetemp1.txt) do (
set /a size1=%%i
)
for /f "tokens=3 delims= " %%i in (sizetemp2.txt) do (
set /a size2=%%i
)
del comparetemp1.txt
del comparetemp2.txt
del sizetemp1.txt
del sizetemp2.txt
if !size1! gtr !size2! (
exit /b 1
) else if !size1! lss !size2! (
exit /b 2
) else (
exit /b 0
)
然后再建立一个批处理文件来调用它,把它命名为test.bat
@echo off
call comparesize.bat No1filepath No2filepath
echo %errorlevel%
pause
echo将会输出较大文件的编号,相同的时候输出0.