Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Even doing "setlocal enabledelayedexpansion", 'fc' still returned wrong value  (Read 4068 times)

0 Members and 1 Guest are viewing this topic.

Stan Huang

    Topic Starter


    Beginner

    • Experience: Experienced
    • OS: Windows 7
    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

    Salmon Trout

    • Guest
    You 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.

    @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

    Salmon Trout

    • Guest
    That's

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

    Stan Huang

      Topic Starter


      Beginner

      • Experience: Experienced
      • OS: Windows 7
      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?

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      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.
      I was trying to dereference Null Pointers before it was cool.