check if command was successful in a batch file
Asked Answered
H

6

67

How within a batch file to check if command

start "" javaw -jar %~p0/example.jar

was successful or produced an error?

I want to use if/else statements to echo this info out.

Hacking answered 4/2, 2013 at 16:55 Comment(0)
S
53

You can use

if errorlevel 1 echo Unsuccessful

in some cases. This depends on the last command returning a proper exit code. You won't be able to tell that there is anything wrong if your program returns normally even if there was an abnormal condition.

Caution with programs like Robocopy, which require a more nuanced approach, as the error level returned from that is a bitmask which contains more than just a boolean information and the actual success code is, AFAIK, 3.

Superelevation answered 4/2, 2013 at 16:57 Comment(7)
This is the right answer as it is more explicit than checking %errorlevel% as ERRORLEVEL is not %ERRORLEVEL%.Coopery
@TomášZato: It will trigger on any positive exit code.Superelevation
@Coopery That link is now broken, updated link: devblogs.microsoft.com/oldnewthing/20080926-00/?p=20743Shabuoth
@Coopery what if errorlevel is -1?Dishpan
What about it @marco-sulla?Coopery
@Coopery Well, if errorlevel 1 does not work, since it checks only if errorlevel is greater than 1. IMHO the only safe way to check if there's an error is to check if errorlevel is not equal to zero.Dishpan
Indeed, I just verified that the errorlevel is signed on Windows, and an exit /b -1 does not result in errorlevel to be 255 (like it would have been the case in DOS times), but -1. Anyway, the point of this answer was to use errorlevel instead of %errorlevel%, and the comparison was just an example IMO.Coopery
S
91

This likely doesn't work with start, as that starts a new window, but to answer your question:

If the command returns a error level you can check the following ways

By Specific Error Level

commandhere
if %errorlevel%==131 echo do something

By If Any Error

commandhere || echo what to do if error level ISN'T 0

By If No Error

commandhere && echo what to do if error level IS 0

If it does not return a error level but does give output, you can catch it in a variable and determine by the output, example (note the tokens and delims are just examples and would likely fail with any special characters)

By Parsing Full Output

for /f "tokens=* delims=" %%a in ('somecommand') do set output=%%a
if %output%==whateveritwouldsayinerror echo error

Or you could just look for a single phrase in the output like the word Error

By Checking For String

commandhere | find "Error" || echo There was no error!
commandhere | find "Error" && echo There was an error!

And you could even mix together (just remember to escape | with ^| if in a for statement)

Hope this helps.

Sayre answered 4/2, 2013 at 17:33 Comment(2)
How to check not equal?Comstockery
This should be the accepted answerDishpan
S
53

You can use

if errorlevel 1 echo Unsuccessful

in some cases. This depends on the last command returning a proper exit code. You won't be able to tell that there is anything wrong if your program returns normally even if there was an abnormal condition.

Caution with programs like Robocopy, which require a more nuanced approach, as the error level returned from that is a bitmask which contains more than just a boolean information and the actual success code is, AFAIK, 3.

Superelevation answered 4/2, 2013 at 16:57 Comment(7)
This is the right answer as it is more explicit than checking %errorlevel% as ERRORLEVEL is not %ERRORLEVEL%.Coopery
@TomášZato: It will trigger on any positive exit code.Superelevation
@Coopery That link is now broken, updated link: devblogs.microsoft.com/oldnewthing/20080926-00/?p=20743Shabuoth
@Coopery what if errorlevel is -1?Dishpan
What about it @marco-sulla?Coopery
@Coopery Well, if errorlevel 1 does not work, since it checks only if errorlevel is greater than 1. IMHO the only safe way to check if there's an error is to check if errorlevel is not equal to zero.Dishpan
Indeed, I just verified that the errorlevel is signed on Windows, and an exit /b -1 does not result in errorlevel to be 255 (like it would have been the case in DOS times), but -1. Anyway, the point of this answer was to use errorlevel instead of %errorlevel%, and the comparison was just an example IMO.Coopery
S
20

Most commands/programs return a 0 on success and some other value, called errorlevel, to signal an error.

You can check for this in you batch for example by:

call <THE_COMMAND_HERE>
if %ERRORLEVEL% == 0 goto :next
echo "Errors encountered during execution.  Exited with status: %errorlevel%"
goto :endofscript

:next
echo "Doing the next thing"

:endofscript
echo "Script complete"
Shantishantung answered 10/2, 2014 at 18:3 Comment(0)
B
8

Goodness I had a hard time finding the answer to this... Here it is:

cd thisDoesntExist
if %errorlevel% == 0 (
  echo Oh, I guess it does
  echo Huh.
)
Bilek answered 16/8, 2017 at 15:20 Comment(0)
T
2

I don't know if javaw will write to the %errorlevel% variable, but it might.

echo %errorlevel% after you run it directly to see.

Other than that, you can pipe the output of javaw to a file, then use find to see what the results were. Without knowing the output of it, I can't really help you with that.

Tague answered 4/2, 2013 at 17:1 Comment(0)
B
0

So logically, this would test for success:

if not errorlevel 1 echo success

Well, unless there's a negative exit code, which would be unusual. This also has the bonus of seeing what the errorlevel is.

if %errorlevel%==0 echo success
Barbe answered 13/7, 2022 at 17:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.