Batch file 批次错误级别结果与CMD不同?

Batch file 批次错误级别结果与CMD不同?,batch-file,errorlevel,Batch File,Errorlevel,为什么ERRORLEVEL在这两种情况下表现不同 从命令行: Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. C:\>aescrypt.exe -v 2> NUL C:\>echo %errorlevel% 9009 与批处理文件相比: @echo off set /P C="> "? set or= if "%C%"=="a" set or=1 if

为什么ERRORLEVEL在这两种情况下表现不同

从命令行:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>aescrypt.exe -v 2> NUL

C:\>echo %errorlevel%
9009
与批处理文件相比:

@echo off

set /P C="> "?

set or=
if "%C%"=="a" set or=1
if "%C%"=="A" set or=1
if defined or (
    aescrypt.exe -v 2> NUL
    echo %errorlevel%
)
结果:

> a
1
删除“@echo off”并查看代码是如何执行的。您可能会发现示例2中的errorlevel是“if defined”的结果

此外,请尝试以下方法:

@echo off

设置/P C=“>”?

设置或=

如果/i“%C%”==“a”集或=1

如果未定义或跳过

aescrypt.exe-v2>NUL

echo%errorlevel%


:跳过

谢谢,我一定错过了/I标志,这正是我所需要的。我想我的问题是“为什么当新错误发生时错误级别没有改变?”这对我来说不是一个紧迫的问题。再次感谢!