1.

Solve : Even doing "setlocal enabledelayedexpansion", 'fc' still returned wrong value?

Answer»

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 255You don't NEED setlocal enabledelayedexpansion twice.

You did not change %errorlevel% to !errorlevel! where I have shown.

From my previous answer

Quote

2. Use exclamation marks, not percent signs, with variable i.e. !errorlevel! not %errorlevel%

You should not use :: to start a COMMENT. This is wrong and will break code blocks. Use REM.

You should start many threads which are all about the same code.

Quote from: Stan Huang on May 31, 2013, 02:23:13 AM
@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
That's

Quote from: Salmon Trout on May 31, 2013, 02:45:54 AM
You should not start many threads which are all about the same code.
For "You should not start many threads which are all about the same code.", do you mean that I should try to use interaction instead of recursion?Quote from: Stan Huang on June 02, 2013, 06:49:48 PM
For "You should not start many threads which are all about the same code.", do you mean that I should try to use interaction instead of recursion?

No. He means you shouldn't start multiple forum threads about the same code.


Discussion

No Comment Found