# Solved: errorlevel bacth not working



## tom2000 (Feb 22, 2009)

This code is not working

set room=102
for /L %%n IN (0, 1, 9) DO (
ping pc%room%x0%%n -w 1 -n 1|find "answer"
if %ERRORLEVEL% equ 1 goto a
echo "some output a"
:a
echo "some output b"
)

Additional note: I want to test the availibility of a computer. The computernames are build up to sample pc102x00, pc102x01, pc102x03,...

Running the script on a MS W2k3 Server the output is for each run "some output a" and "some output b". The value of errorlevel is always 0. Removing the loop (for) the script works fine.


----------



## TheOutcaste (Aug 8, 2007)

Welcome to TSG!

Because you are in a loop, the *%Errorlevel%* value is expanded first, and the initial value will be used for each iteration, it won't be updated. So you have to use Delayed Expansion, and !ERRORLEVEL! instead of %ERRORLEVEL%

Also, after the *echo "some output a"* statement it will fall through to the *echo "some output b"*, so both will be displayed if the system replies. You can combine these in one If/Else statement.

I've also added the */I* switch to the Find command so it won't be case sensitive, so you can use *Reply* or *reply* for the "answer"

This should work a bit better. Will display *"some output b"* if no reply, *"some output a"* if there is a reply:

```
Setlocal EnableDelayedExpansion
set room=102
For /L %%n IN (0, 1, 9) DO (
ping pc%room%x0%%n -w 1 -n 1|find /I "Reply"
if !ERRORLEVEL! equ 1 (
echo "some output b"
) Else (
echo "some output a"
))
```
HTH

Jerry


----------



## tom2000 (Feb 22, 2009)

...works fine, thanks a lot!!!


----------

