ERRORLEVEL在for循环(批号视窗) [英] Errorlevel in a For loop (batch windows)

查看:443
本文介绍了ERRORLEVEL在for循环(批号视窗)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的Windows批处理code:

I have the following windows batch code:

for %%i in (iidbms iigcc iigcd dmfacp dmfrcp rmcmd qwerty) do (
  tasklist | findstr /i %%i
  echo %errorlevel%
  if %errorlevel% == 0 (echo %%i ok process found %errorlevel%)
  if %errorlevel% == 1 (echo %%i no process found %errorlevel%)
)

但我希望这是行不通的。

But it doesn't work as I expect.

所有进程的名字 iidbms iigcc iigcd dmfacp dmfrcp rmcmd 的是真实的,他们被发现,而不是 QWERTY 的是创造一个而不是应该找到它,所以应打印的不过程中发现1,但它不,它总是输出0。

All the name processes iidbms, iigcc, iigcd, dmfacp, dmfrcp, rmcmd are real, and they are found, instead qwerty is an invented one and should not find it, so should print " no process found 1", but it doesn't, it always prints 0.

但我注意到的是,如果我运行任务列表| FINDSTR /我QWERTY 从DOS提示符,刚过就有了%ERRORLEVEL% = 1。

But what I have noted is that if I run the tasklist | findstr /i qwerty from the dos prompt, just after there is that the %errorlevel% = 1.

什么样的​​答案可能是或更好的是什么?

What sort of answer could be or better is?

推荐答案

如果ERRORLEVEL返回TRUE如果返回code比指定的错误级别相同或更高。在例子中,由于0小于1,如果实际误差code是0或高于第一错误级别语句将始终是真的。你想要的是来测试ERRORLEVEL 1首。

IF ERRORLEVEL returns TRUE if the return code was equal to or higher than the specified errorlevel. In your example, since 0 is lower than 1, the first errorlevel statement will always be true if the actual error code is 0 or above. What you want is to test for errorlevel 1 first.

例如:

for %%i in (iidbms iigcc iigcd dmfacp dmfrcp rmcmd qwerty) do (
    tasklist | findstr /i %%i
    if errorlevel 0 if not errorlevel 1 echo process
    if errorlevel 1 if not errorlevel 2 echo process not found
)

另一个问题是,如果你想从内部回声实际的错误级别的循环。由于变量循环开始前得到解决,呼应%ERRORLEVEL%将永远回响0。如果你想在执行时呼应的价值,你需要修改片段,像这样:

Another issue is if you want to echo the actual errorlevel from within the for loop. Since variables are resolved before the start of the loop, echoing %errorlevel% will always echo 0. If you want to echo the value at the execution time, you need to modify the snippet like so:

setlocal enabledelayedexpansion
for %%i in (iidbms iigcc iigcd dmfacp dmfrcp rmcmd qwerty) do (
    tasklist | findstr /i %%i
    if errorlevel 0 if not errorlevel 1 echo %%i ok process found !errorlevel!
    if errorlevel 1 if not errorlevel 2 echo %%i no process found !errorlevel!
)

这篇关于ERRORLEVEL在for循环(批号视窗)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆