English 中文(简体)
For循环中的错误级别(Windows批处理)
原标题:Errorlevel in a For loop (Windows batch)

我有以下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%)
)

但它并没有像我预期的那样奏效。

所有的名称进程iidbmsiigcciigcddmfacp都是真实的,并且它们被发现了,而qwerty是一个虚构的进程,不应该找到它,所以应该打印“未找到进程1”,但它没有,它总是打印0。

但我注意到的是,如果我从dos提示符运行任务列表|findstr/I qwerty,紧接着%errorlevel%=1。

什么样的答案可以是或者更好?

最佳回答

如果返回代码等于或高于指定的错误级别,则IF ERRORLEVEL返回TRUE。在您的示例中,由于0低于1,因此如果实际错误代码为0或更高,则第一个errorlevel语句将始终为true。您想要的是首先测试错误级别1。

例如:

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
)

另一个问题是,如果您想从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!
)
问题回答

添加

setlocal EnableDelayedExpansion

to the start of your script, then use !errorlevel! instead of %errorlevel%

Delayed Expansion will cause variables to be expanded at execution time rather than at parse time

~http://ss64.com/nt/delayedexpansion.html

The answer to another question that pointed me in the right direction:
Errorlevel of command executed by batch for loop

您可以使用vbscript,

NumArgs = WScript.Arguments.Count
strComputer="."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\" & strComputer & "
ootcimv2")
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

用法:

C:	est>cscript //nologo test.vbs explorer spool svchost
found:svchost
found:svchost
found:svchost
found:svchost
found:svchost
found:explorer
found:svchost
found:spool




相关问题
Why running a service as Local System is bad on windows?

I am trying to find out the difference between difference service account types. I tumbled upon this question. The answer was because it has powerful access to local resources, and Network Service ...

Programmatically detect Windows cluster configuration?

Does anyone know how to programatically detect that a Windows server is part of a cluster? Further, is it possible to detect that the server is the active or passive node? [Edit] And detect it from ...

get file icon for Outlook appointment (.msg)

I ve read Get File Icon used by Shell and the other similar posts - and already use SHFileInfo to get the associated icon for any given extension, and that works great. However, Outlook uses ".msg" ...

Identifying idle state on a windows machine

I know about the GetLastInputInfo method but that would only give me the duration since last user input - keyboard or mouse. If a user input was last received 10 minutes ago, that wouldn t mean the ...

Terminating a thread gracefully not using TerminateThread()

My application creates a thread and that runs in the background all the time. I can only terminate the thread manually, not from within the thread callback function. At the moment I am using ...

热门标签