批处理文件For循环不起作用

时间:2017-11-09 12:07:54

标签: windows batch-file cmd

我试图迭代下面的循环,同时执行如果发生错误循环应该进入IF条件并退出但循环永远不会进入IF条件总是执行ELSE条件,检查%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"......
    )
)

1 个答案:

答案 0 :(得分:0)

(个括号)内的变量只展开一次,因此%ERRORLEVEL%将被解析为for循环时的值。您必须启用延迟展开并使用!ERRORLEVEL!,或者只使用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" . . .
  )
)
相关问题