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.
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.
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.
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 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 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.
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.
%errorlevel%
as ERRORLEVEL is not %ERRORLEVEL%. –
Coopery 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 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 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"
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.
)
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.
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
© 2022 - 2024 — McMap. All rights reserved.
%errorlevel%
as ERRORLEVEL is not %ERRORLEVEL%. – Coopery