2

I have two cmd files. child.cmd:

@echo off
exit 1

parent.cmd:

@echo off
cmd /C child.cmd
if %errorlevel% EQU 0 (
   echo OK
) else (
   echo ERROR
)

If to run parent.cmd, then ERROR will be printed.

But if a little change parent.cmd, then OK will be printed:

@echo off
if "YES" EQU "YES" (
   cmd /C child.cmd
   if %errorlevel% EQU 0 (
      echo OK
   ) else (
      echo ERROR
   )
)

Why OK is printed in the second example?

4

2 回答 2

5

在您需要delayed expansion访问的代码块内%variables%

 @echo off &setlocal enabledelayedexpansion
 if !errorlevel! EQU 0 (
于 2013-09-09T12:55:11.800 回答
1

您也可以使用此语法而无需延迟扩展:

if errorlevel 1 if not errorlevel 2 ( echo error )
于 2013-09-09T13:14:35.973 回答