DOS下比較兩個檔案的大小
阿新 • • 發佈:2019-02-14
首先建立一個批處理檔案,命名為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.