IF 内的错误级别

发布于 2024-10-06 03:28:03 字数 1767 浏览 0 评论 0原文

刚刚偶然发现了 %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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千纸鹤带着心事 2024-10-13 03:28:03

尝试在批处理文件开头使用 setlocalenabledelayedexpansion,并在 IF 内使用 !ERRORLEVEL!。这似乎对我有用:

@echo off
setlocal enabledelayedexpansion
dir nul
echo %ERRORLEVEL%
if .1.==.1. (
  urklbkrlksdj - not a command
  echo %ERRORLEVEL%
  echo !ERRORLEVEL!
)

Try using setlocal enabledelayedexpansion at the start of your batch file, and !ERRORLEVEL! inside your IF. This seems to work for me:

@echo off
setlocal enabledelayedexpansion
dir nul
echo %ERRORLEVEL%
if .1.==.1. (
  urklbkrlksdj - not a command
  echo %ERRORLEVEL%
  echo !ERRORLEVEL!
)
拧巴小姐 2024-10-13 03:28:03

if errorlevel 工作时不会延迟扩展,但工作方式类似于

if %errorlevel% <= Some_Value ...

@echo off

::sets errorlevel to 0
(call )
if "1" == "1" (
    rem sets errorlevel to 5
    cmd /c exit 5
    if errorlevel 4 echo this will be printed
    if errorlevel 5 echo this will be printed
    rem :::: you can use this ::::::::::::::
    if errorlevel 5 if not errorlevel 6 echo this will be printed ONLY when the errorlevel is 5
    rem :::::::::::::::::::::::::::::::::::::
    if errorlevel 6 echo this will not be printed

)

if errorlevel works without delayed expansion but works in manner similar to

if %errorlevel% <= Some_Value ... :

@echo off

::sets errorlevel to 0
(call )
if "1" == "1" (
    rem sets errorlevel to 5
    cmd /c exit 5
    if errorlevel 4 echo this will be printed
    if errorlevel 5 echo this will be printed
    rem :::: you can use this ::::::::::::::
    if errorlevel 5 if not errorlevel 6 echo this will be printed ONLY when the errorlevel is 5
    rem :::::::::::::::::::::::::::::::::::::
    if errorlevel 6 echo this will not be printed

)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文