Errorlevel in a For loop (batch windows)

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.

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 print 0.

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

What sort of answer could be or better is?

This question and answers originated from www.stackoverflow.com
Question by (10/15/2010 12:37:32 PM)

Answer

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.

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:

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!
)
Answer by

Find More Answers
Related Topics  windows  batch  errorlevel
Related Questions
  • Windows batch-file errorlevel question

    I've got a batch file that parses a bunch of file names out of a text file and concatenates them into a single strong - it was previously discussed here . However, I don't want the string to contain…
  • Batch %errorlevel% returns 0 in FOR loop

    I am trying to make a loop that will go through a set of text files in specified directory searching for a string. The result is reported based on whether the string is found. But the %errorlevel% a…
  • Killing a process in Batch and reporting on success

    i have the following batch file, which terminates the iTunes program so, that if i connect my iPod, it's not going to sync it. (I know you can set this up in iTunes.) @echo off :kill cls taskkill…
  • Batch ERRORLEVEL results different from CMD?

    Why does ERRORLEVEL behave differently in these two circumstances? From the command line: Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. C:\>aescrypt.exe -…
  • Errorlevel of command executed by batch for loop

    The following code always displays 0 as the errorlevel, but when the copy command is done outside of the for loop command it returns a non zero errorlevel. for /f "usebackq delims=" %%x in (`copy…
  • ERRORLEVEL inside IF

    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 …
  • Escaping strings in a Windows Batch For Loop

    I'm having problems with the following code: FOR /f "tokens=*" %%A in (%MYFILE_PATH%) do ( [other stuff] echo %%A>> "%MYFILE_PATH%.scratch" ) The file being read has XML in it and w…
  • Why does cmd.exe have different errorlevel behavior on a 64-bit machine?

    If I make a batch script named temp.bat (for example) containing: exit /b 1 When I run it in various ways, I get different behavior on my 32-bit XP system vs. a 64-bit XP system. On 32-bit…
  • How to set ERRORLEVEL in SSIS?

    I have a batch file that runs an SSIS job. I have no knowledge of how the SSIS job runs, I took over a project involving it. The batch file uses %ERRORLEVEL% to detect errors that occur within t…
  • How to ensure full path in Windows batch FOR loop

    I've created a generic batch (or Windows Command) file that lets me loop through the contents of a directory and call a command for each item. IF a%1==a ( set _DIR="%CD%") ELSE ( set _DIR="%~1") …