windows For 循环中的错误级别(批处理窗口)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3942265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Errorlevel in a For loop (batch windows)
提问by JRL
I have the following windows batch code:
我有以下 Windows 批处理代码:
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.
但它不像我预期的那样工作。
All the name processes iidbms, iigcc, iigcd, dmfacp, dmfrcp, rmcmdare real, and they are found, instead qwertyis an invented one and should not find it, so should print " no process found 1", but it doesn't, it always prints 0.
所有名称进程iidbms、iigcc、iigcd、dmfacp、dmfrcp、rmcmd都是真实的,并且它们被找到,而不是qwerty是一个发明的,不应该找到它,所以应该打印“没有找到进程 1”,但它没有,它总是打印 0。
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.
但是我注意到的是,如果我tasklist | findstr /i qwerty
从 dos 提示符运行,则紧随其后的是%errorlevel%
= 1。
What sort of answer could be or better is?
什么样的答案可能或更好?
回答by Tim Abell
Add
添加
setlocal EnableDelayedExpansion
to the start of your script, then use
!errorlevel!
instead of %errorlevel%
到脚本的开头,然后使用
!errorlevel!
而不是%errorlevel%
Delayed Expansion will cause variables to be expanded at execution time rather than at parse time
延迟扩展将导致变量在执行时而不是在解析时扩展
The answer to another question that pointed me in the right direction: https://stackoverflow.com/a/6658935/10245
另一个问题的答案为我指明了正确的方向:https: //stackoverflow.com/a/6658935/10245
回答by JRL
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.
如果返回代码等于或高于指定的错误级别,则 IF ERRORLEVEL 返回 TRUE。在您的示例中,由于 0 小于 1,如果实际错误代码为 0 或更高,则第一个 errorlevel 语句将始终为真。您想要的是首先测试错误级别 1。
E.g.:
例如:
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
)
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:
另一个问题是,如果您想从 for 循环内回显实际错误级别。由于变量是在循环开始之前解析的,因此回显 %errorlevel% 将始终回显 0。如果要在执行时回显值,则需要像这样修改代码片段:
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!
)
回答by ghostdog74
You can use vbscript,
您可以使用 vbscript,
NumArgs = WScript.Arguments.Count
strComputer="."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process")
For Each objProcess in colProcessList
For i=0 To NumArgs-1
If InStr( objProcess.Name ,WScript.Arguments(i) ) > 0 Then
WScript.Echo "found:" & WScript.Arguments(i)
End If
Next
Next
Usage:
用法:
C:\test>cscript //nologo test.vbs explorer spool svchost
found:svchost
found:svchost
found:svchost
found:svchost
found:svchost
found:explorer
found:svchost
found:spool