# errorlevel batch not working



## goldhat (Aug 12, 2007)

thi code is not working


```
@echo off
SC query messenger | FIND "STATE"
IF ERRORLEVEL 0 goto m
IF ERRORLEVEL 1 goto start
:start
cls
sc config messenger start= demand 
net start messenger 
goto m
@echo off
:m 
Cls 
echo MESSENGER 
set /p n=Users IP: 
set /p m=Message: 
net send %n% %m% 
Goto m
```
the problem is the command if errorlevel 0 goto m

the service is not started so.....
it should be errorlevel 1 making it go start but now it just goes straight to
line labeled m


----------



## JohnWill (Oct 19, 2002)

What makes you think that "STATE" is unique? How about looking for something like "STOPPED", which is a bit more unique in the status you're looking at?


----------



## goldhat (Aug 12, 2007)

ok...... thx but it still doesnt work

anybody know


----------



## foofoo (Sep 6, 2007)

goldhat 

errorlevel must be checked in descending order.  
The errorlevel check acts like a greater-than-or-equal-to test. If errorlevel is 1, the check of errorlevel = 1 succeeds, as does the check for errorlevel=0. 
The zero test is always true, so you always are going to GOTO M.  

Change your logic as follows and it will work :up: :

SC query messenger | FIND "STATE"
IF errorlevel 1 goto start
IF errorlevel 0 goto m

Additional note: if errorlevel is greater than 1, you will go to START. Adding if errorlevel 2 goto OhOhWhatHappenedHereBetterReactToIt is not a bad practice should the return codes from FIND ever return other than 1 or 0. The test for 2 will be true for ANY value other than one or zero.


----------



## goldhat (Aug 12, 2007)

thx alot


----------



## goldhat (Aug 12, 2007)

the new code doesnt work


```

```
@echo off
SC query messenger | FIND "STATE"
IF errorlevel 1 goto start
IF errorlevel 0 goto m
:start
cls
sc config messenger start= demand 
net start messenger 
goto m
@echo off
:m 
Cls 
echo MESSENGER 
set /p n=Users IP: 
set /p m=Message: 
net send %n% %m% 
Goto m


----------



## foofoo (Sep 6, 2007)

You need to define what you mean by "not work". This is no longer an errorlevel problem.

SC query messenger | find "STATE"
The FIND finds the word "STATE" in the output, returns errorlevel = zero.

if errorlevel 1 goto start
This is false, so it does not go to start.

if errorlevel 0 goto m
This is true, so execution continues at m.

On my machine, the find command finds "STATE" in the following result:
" STATE : 1 STOPPED " 
errorlevel is set to zero, and it SHOULD proceed to M, per your logic.

This works as expected.
Hint: insert "@echo %errorlevel%" after the first SC command so you can see the results.

You can try this at the command line:
echo "STATE : 1 STOPPED" | find "STATE"
@echo %errorlevel%
Outputs a 0

echo "STAT : 1 STOPPED" | find "STATE"
@echo %errorlevel%
Outputs a 1


SUGGESTION: Try this:
@echo OFF
SC query messenger | find "STATE" > foo.foo
echo %errorlevel%
type foo.foo | find "1"
echo %errorlevel%
if errorlevel 1 goto m
REM Testing of errorlevel not needed here! 0 test is always true!
REM if errorlevel 0 goto m
goto start

:start
echo START
sc config messenger start= demand
net start messenger
pause
goto m

:m
echo MESSENGER
set /p n=Users IP:
set /p m=Message:
net send %n% %m%
pause
goto m


I think you will find the output of the FIND is not what you are anticipating.  

If it is what you expect, maybe you can explain exactly what does not work?  

I think STATE is not the string you should be trying to find.


----------



## goldhat (Aug 12, 2007)

perfect it works but it displays 0
1

even when the service is started 

anyway to fix that ?


----------



## foofoo (Sep 6, 2007)

[edit to remove comment] don't want a tongue lashing.


----------



## foofoo (Sep 6, 2007)

Um, yes?


----------

