Below is my program for comparing files and folders under two specified folders. It almost works well except one issue. That is, after 'fc /b' compared two different files, %errorlevel% is 0. I met the same phenomenon and it was due to not set "enabledelayedexpansion". This time I am sure I did it. How come?
@echo off
call :cmpdir %1 %2
if %errorlevel% equ 0 (echo two dirs are the same) else (
echo two dirs differ
)
goto :EOF
:cmpdir dir1 dir2
setlocal enabledelayedexpansion
pushd %2
for /f "delims=" %%A in ('cd') do set dir2=%%A
echo dir2=%dir2%
popd
pushd %1
FOR %%G in (*) DO (
echo "%%~fG <<==>> %dir2%\%%~nxG"
if not exist %dir2%\%%~nxG (
echo %dir2%\%%~nxG disappeared
goto :cmpdir_err
)
setlocal enabledelayedexpansion
fc /b "%%~fG" "%dir2%\%%~nxG" > nul
if %errorlevel% neq 0 (
echo %dir2%\%%~nxG corrupted
goto :cmpdir_err
)
)
:: echo "files done, then subdir"
FOR /d %%G in (*) DO (
echo %%~fG
if not exist %dir2%\%%~nxG (
echo %dir2%\%%~nxG disappeared
exit /b 255
)
call :cmpdir %%~fG %dir2%\%%~nxG
if %errorlevel% equ 255 goto :cmpdir_err
)
:cmpdir_ok
exit /b 0
:cmpdir_err
popd
exit /b 255