0

I am trying to iterate over below loop , while executing if error occurs loop should go to IF condition and exit but loop never goes to IF condition always ELSE condition is executed , is there any problem with check of %errorlevel% ?

SET list=clean all cfg1 

CD "C:\SW\CONS\build"

FOR %%a IN (%list%) DO (
    CALL build.bat %%a
    IF %errorlevel% neq 0 (
        echo Exiting the loop
        EXIT /B %errorlevel%
    ) ELSE  (
        echo Successfully executed "%%a"......
    )
)
lit
  • 14,456
  • 10
  • 65
  • 119

1 Answers1

1

Variables inside (parentheses) are expanded only once, so %ERRORLEVEL% will be replaced with whatever value it had when the for loop is parsed. You must either enable delayed expansion and use !ERRORLEVEL!, or just use if errorlevel:

for %%a in (%LIST%) do (
  call Build.bat %%a
  if errorlevel 1 (
    echo Exiting the loop
    goto :eof
  ) else (
    echo Successfully executed "%%a" . . .
  )
)
AlexP
  • 4,370
  • 15
  • 15