IF 内的错误级别
刚刚偶然发现了 %ERRORLEVEL%
的奇怪现象,想看看是否有人知道原因以及是否有解决方法。本质上,似乎在 if 语句内执行的命令没有设置 %ERRORLEVEL%
变量。 ERRORLEVEL
(如 IF ERRORLEVEL 1
中所示,与 IF %ERRORLEVEL% EQU 1
不同)检查似乎仍然可以正常工作,所以我可能可以解决这个问题,但是能够打印错误级别仍然很好。用于调试或其他什么。
@echo off
Set TESTVAR=1
tasklist | find /I "IsntRunning.exe" > NUL
echo OUTSIDE_IF %ERRORLEVEL%
ThisWillSetErrorLevelTo9009ieNotRecognizedCommand
tasklist | find /I "IsntRunning.exe" > NUL
echo OUTSIDE_IF %ERRORLEVEL%
ThisWillSetErrorLevelTo9009ieNotRecognizedCommand
IF %TESTVAR% EQU 1 (
Set ERRORLEVEL=
tasklist | find /I "IsntRunning.exe" > NUL
echo INSIDE_IF ERRORLEVEL %ERRORLEVEL%
IF ERRORLEVEL 1 (
echo INSIDE_IF2 ERRORLEVEL GREQ 1 %ERRORLEVEL%
)
IF ERRORLEVEL 2 (
echo INSIDE_IF2 ERRORLEVEL GREQ 2 %ERRORLEVEL%
)
IF ERRORLEVEL 3 (
echo INSIDE_IF2 ERRORLEVEL GREQ 3 %ERRORLEVEL%
)
)
tasklist | find /I "IsntRunning.exe" > NUL
echo OUTSIDE_IF ERRORLEVEL %ERRORLEVEL%
@echo on
将其放入批处理文件并运行它会产生以下输出:
C:\Users\用户名\Documents\work>test.bat
OUTSIDE_IF 1
'ThisWillSetErrorLevelTo9009ieNotRecognizedCommand' 未被识别为内部或外部命令, 可运行的程序或批处理文件。
OUTSIDE_IF 1
'ThisWillSetErrorLevelTo9009ieNotRecognizedCommand' 未被识别为内部或外部命令, 可运行的程序或批处理文件。
INSIDE_IF 错误级别 9009
INSIDE_IF2 错误级别 GREQ 1 9009
OUTSIDE_IF 错误级别 1
相关文章:
Just stumbled into a weird thing with %ERRORLEVEL%
and wanted to see if anyone knows why and if there's a way to fix it. Essentially, it seems as if commands executed inside if statements don't set the %ERRORLEVEL%
variable. The ERRORLEVEL
(as in IF ERRORLEVEL 1
, which is different from IF %ERRORLEVEL% EQU 1
) check seems to still work fine though, so I can probably work around it, but it would still be nice to be able to print the error level. For debugging or whatever.
@echo off
Set TESTVAR=1
tasklist | find /I "IsntRunning.exe" > NUL
echo OUTSIDE_IF %ERRORLEVEL%
ThisWillSetErrorLevelTo9009ieNotRecognizedCommand
tasklist | find /I "IsntRunning.exe" > NUL
echo OUTSIDE_IF %ERRORLEVEL%
ThisWillSetErrorLevelTo9009ieNotRecognizedCommand
IF %TESTVAR% EQU 1 (
Set ERRORLEVEL=
tasklist | find /I "IsntRunning.exe" > NUL
echo INSIDE_IF ERRORLEVEL %ERRORLEVEL%
IF ERRORLEVEL 1 (
echo INSIDE_IF2 ERRORLEVEL GREQ 1 %ERRORLEVEL%
)
IF ERRORLEVEL 2 (
echo INSIDE_IF2 ERRORLEVEL GREQ 2 %ERRORLEVEL%
)
IF ERRORLEVEL 3 (
echo INSIDE_IF2 ERRORLEVEL GREQ 3 %ERRORLEVEL%
)
)
tasklist | find /I "IsntRunning.exe" > NUL
echo OUTSIDE_IF ERRORLEVEL %ERRORLEVEL%
@echo on
Putting that in a batch file and running it produces this output:
C:\Users\username\Documents\work>test.bat
OUTSIDE_IF 1
'ThisWillSetErrorLevelTo9009ieNotRecognizedCommand' is not recognized as an internal or external command,
operable program or batch file.
OUTSIDE_IF 1
'ThisWillSetErrorLevelTo9009ieNotRecognizedCommand' is not recognized as an internal or external command,
operable program or batch file.
INSIDE_IF ERRORLEVEL 9009
INSIDE_IF2 ERRORLEVEL GREQ 1 9009
OUTSIDE_IF ERRORLEVEL 1
Relevant articles:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在批处理文件开头使用
setlocalenabledelayedexpansion
,并在IF
内使用!ERRORLEVEL!
。这似乎对我有用:Try using
setlocal enabledelayedexpansion
at the start of your batch file, and!ERRORLEVEL!
inside yourIF
. This seems to work for me:if errorlevel
工作时不会延迟扩展,但工作方式类似于if %errorlevel% <= Some_Value ...
:if errorlevel
works without delayed expansion but works in manner similar toif %errorlevel% <= Some_Value ...
: