@echo off
setlocal enableextensions enabledelayedexpansion
rem Author: rubble@
rem 17:55 2009-4-14 Version: 0.4
set dira=%~1
set dirb=%~2
if "%1" == "" (
echo Instructions: Please indicate two directories on the command line parameters, or enter them as prompted here.
echo command line parameter format: batch name ^<directory 1^> ^<directory 2^>
echo.
set /p dira=Please enter the name of the first directory (can be drag-and-drop and enter Enter):
)
echo.
if "%2" == "" (
echo Instructions: Please indicate two directories on the command line parameters, or enter them as prompted here.
echo command line parameter format: batch name ^<directory 1^> ^<directory 2^>
echo.
set /p dirb=Please enter the name of the second directory (can be drag-and-drop and enter Enter):
)
if "%dira%" == "" exit /b
if "%dirb%" == "" exit /b
for /F "delims=. tokens=1" %%A in ("%TIME%") do set startTime=%%A
for /F "delims=: tokens=1,2,3" %%A in ("%startTime%") do (
set startH=%%A
set startM=%%B
set startS=%%C
)
set TEMPDIR=TEMPMD5\
rd /Q /S %TEMPDIR% 1>NUL 2>NUL
md %TEMPDIR%
set mdfive=
rem Number of files processed
set count=0
Total number of files in the rem folder %dira% and %dirb%
set total_count=0
rem Number of temporary files that have been processed
set tmpf_count=0
rem The total number of temporary files, 5 are known to be. The rest are the number of md5 values
set tmpf_total_count=5
echo Step 1/2, calculating, please wait...
echo is comparing md5 values for files in %dira% and %dirb% > %mdfive%
set list_fn_md=%TEMPDIR%list_fn_md
set list_fn_nmd=%TEMPDIR%list_fn_nmd
set list_fn_oa=%TEMPDIR%list_fn_oa
set list_fn_ob=%TEMPDIR%list_fn_ob
set list_all_md=%TEMPDIR%list_all_md
call :COUNTFILES %dira%
call :COUNTFILES %dirb%
for /f "delims=" %%a in ('dir /b /a:-d "%dira%\*"') do (
set fn=%%~nxa
for /f "tokens=1 delims= " %%A in ('md5sum "%dira%\%%a"') do set mda=%%A
set /a count=!count!+1
call :PROGRESS !count! !total_count!
if "!mda:~0,1!" == "\" set mda=!mda:~1,1024!
rem takes !mda! as the file name, and records the file with md5 as !mda!
call :ADDMDFN "%TEMPDIR%!mda!" "%dira%\!fn!"
rem records the md5 value to the file to ensure that it does not repeat
call :ADDMDLIST !mda!
if exist "%dirb%\!fn!" (
for /f "tokens=1 delims= " %%A in ('md5sum "%dirb%\!fn!"') do set mdb=%%A
set /a count=!count!+1
call :ADDMDFN "%TEMPDIR%!mdb!" "%dirb%\!fn!"
call :PROGRESS !count! !total_count!
if "!mdb:~0,1!" == "\" set mdb=!mdb:~1,1024!
if "!mda!" == "!mdb!" (
rem record file names with the same file name and md5 same file
echo !mda! !mdb! !fn! >> "!list_fn_md!"
) else (
rem records files with the same file name but md5 different
echo !mda! !mdb! !fn! >> "!list_fn_nmd!"
call :ADDMDLIST !mdb!
)
) else (
rem records files that only appear in %dira%
echo !mda! !fn! >> "!list_fn_oa!"
)
)
for /f "delims=" %%a in ('dir /b /a:-d "%dirb%\*"') do (
set fn=%%~nxa
if not exist "%dira%\!fn!" (
for /f "tokens=1 delims= " %%A in ('md5sum "%dirb%\%%a"') do set mdb=%%A
if "!mdb:~0,1!" == "\" set mdb=!mdb:~1,1024!
rem records files that only appear in %dirb%
echo !mdb! !fn! >> "!list_fn_ob!"
rem records the md5 value to the file to ensure that it does not repeat
call :ADDMDLIST !mdb!
call :ADDMDFN "%TEMPDIR%!mdb!" "%dirb%\!fn!"
set /a count=!count!+1
call :PROGRESS !count! !total_count!
)
)
echo Step 2/2, statistics are being conducted, please wait...
echo statistics: >>%mdfive%
echo left: %dira% >> %mdfive%
echo right: %dirb% >> %mdfive%
if not exist "%list_fn_md%" set /a tmpf_total_count-=1
if not exist "%list_fn_nmd%" set /a tmpf_total_count-=1
if not exist "%list_fn_oa%" set /a tmpf_total_count-=1
if not exist "%list_fn_ob%" set /a tmpf_total_count-=1
if not exist "%list_all_md%" set /a tmpf_total_count-=1
echo file that exists in both directories and md5 is the same: >> %mdfive%
call :OUTPUTLIST "%list_fn_md%"
echo ===============================================================================================>> %mdfive%
echo left: %dira% >> %mdfive%
echo right: %dirb% >> %mdfive%
echo files that exist in both directories but md5 are different: >> %mdfive%
call :OUTPUTLIST "%list_fn_nmd%"
echo ===============================================================================================>> %mdfive%
echo files that exist only in directory %dira%: >> %mdfive%
call :OUTPUTLIST "%list_fn_oa%"
echo ===============================================================================================>> %mdfive%
echo files that exist only in directory %dirb%: >> %mdfive%
call :OUTPUTLIST "%list_fn_ob%"
echo ===============================================================================================>> %mdfive%
echo file list summarized with md5: >> %mdfive%
call :OUTPUTLIST_MD "%list_all_md%"
goto ALLDONE
rem counts the number of files in the folder and puts them in the global variable total_count
:COUNTFILES
if %1=="" goto :EOF
for /f %%a in ('dir /b /a:-d "%~1\*"') do (
set /a total_count=!total_count!+1
)
goto :EOF
rem parameter %1 must be double quoted
:OUTPUTLIST
if %1 == "" goto :EOF
if not exist %1 goto :EOF
set tcnt=0
for /f "tokens=1* delims= " %%a in (%~1) do (
echo %%a %%b >> %mdfive%
set /a tcnt+=1
)
echo !tcnt! >> %mdfive%
set /a tmpf_count+=1
call :PROGRESS !tmpf_count! !tmpf_total_count!
goto :EOF
rem parameter %1 must be double quoted
:OUTPUTLIST_MD
if %1 == "" goto :EOF
if not exist %1 goto :EOF
set /a tmpf_count+=1
call :PROGRESS !tmpf_count! !tmpf_total_count!
set md_count=0
for /f "tokens=1* delims= " %%a in (%~1) do (
echo %%a >> %mdfive%
set tcnt=0
for /f "tokens=1* delims= " %%A in (!TEMPDIR!%%a) do (
echo %%A %%B >> %mdfive%
set /a tcnt+=1
)
set /a tmpf_count+=1
call :PROGRESS !tmpf_count! !tmpf_total_count!
echo !tcnt! >> %mdfive%
set /a md_count+=1
echo. >> %mdfive%
)
echo %md_count% >> %mdfive%
goto :EOF
rem function: save the md5 value to a file named !list_all_md!
rem and ensures that duplicate md5 values are not saved
:ADDMDLIST
if %1 == "" goto :EOF
if exist "!list_all_md!" (
rem delims here are either deleted or as shown in the line below, adding a space after the equal sign
rem for /f "tokens=1 delims= " %%a in (!list_all_md!) do (
for /f "tokens=1 " %%a in (!list_all_md!) do (
if "%%a" == "%1" goto :EOF
)
)
echo %1 >> "!list_all_md!"
rem record the total number of temporary files
set /a tmpf_total_count+=1
goto :EOF
rem function: save %2 with %1 as the file name
rem %1: In this application it is md5 value
rem %2: md5 file with value %1
:ADDMDFN
if %1 == "" goto :EOF
if %2 == "" goto :EOF
echo %2 >> %1
goto :EOF
:PROGRESS
set tips=
set backspace=
set /a percent=%1*100/%2
set /a char=%1%%7
set /a bnum=%percent%/10
set /a snum=10-%bnum%
for /L %%a in (1, 1, %bnum%) do set /p=▌<NUL
for /L %%a in (1, 1, %snum%) do set /p= <NUL
set /p=%1/%2 %percent%%% <NUL
set /p=!tips:~%char%,1!<NUL
if "%percent%" == "100" (
echo.
goto :EOF
)
set /p=%backspace%<NUL
goto :EOF
rem :PROGRESS
rem set tips=
rem set backspace=
rem set /a char=%1%%7
rem set /p=!tips:~%char%,1!<NUL
rem set /p=%backspace%<NUL
rem goto :EOF
:ALLDONE
rd /Q /S %TEMPDIR% 1>NUL 2>NUL
for /F "delims=. tokens=1" %%A in ("%TIME%") do set endTime=%%A
for /F "delims=: tokens=1,2,3" %%A in ("%endTime%") do (
set endH=%%A
set endM=%%B
set endS=%%C
)
if "%endM:~0,1%" == "0" set endM=%endM:~1,1%
if "%endS:~0,1%"=="0" set endS=%endS:~1,1%
for /F "delims=: tokens=1,2,3" %%A in ("%startTime%") do (
set startH=%%A
set startM=%%B
set startS=%%C
)
if "%startM:~0,1%"=="0" set startM=%startM:~1,1%
if "%startS:~0,1%"=="0" set startS=%startS:~1,1%
set /A totalTime=%endH%*3600+%endM%*60+%endS%-(%startH%*3600+%startM%*60+%startS%)
echo shared %totalTime% seconds.
echo Start time: %startH%: %startM%: %startS%>>%mdfive%
echo End time: %endH%:%endM%:%endS%>>%mdfive%
echo shared %totalTime% seconds. >>%mdfive%
start %mdfive%
echo
endlocal
@echo on