0

I have a small problem with my batch file, here is my code:

Java -jar ****name.jar -commands****

So incase the user does have java installed it should give proper error code and if it has java installed and the java does not find the file its trying to run it should give proper error code and so on.

If "%Errorlevel%" NEQ "0" (
    For /F "Tokens=*" %%C In ('Net Helpmsg %Errorlevel%') Do (
    Echo Error Level: [Return Code #%Errorlevel% - %%C]
    )
)

I was hoping it would get me correct error codes but it doesn't. Like for example if the user does not have java installed it would say something like this "java is not recognized.." and it should give an correct error code beneath it but unfortunately, it doesn't.

Here is what i have tried:

  • Removed the quotation around Errorlevel so its does not treat it like a string.

I was hoping anyone could point out my mistake?

Anonymous
  • 47
  • 7
  • 1
    Read this first: [SET variable doesn't work in block](http://stackoverflow.com/a/19997402/463115) – jeb Aug 03 '16 at 13:22
  • @jeb That doesn't appear to be a duplicate - the OP isn't setting anything in the IF/FOR. The code seems to do what the OP wants _if_ `ERRORLEVEL` has a value before hand: the problem is more likely that either the (unshown) commands aren't setting errorlevel or it's being clearead before testing. – TripeHound Aug 03 '16 at 13:24
  • @TripeHound That's the cause why I removed my duplicate marker. I didn't read the question carefully enough – jeb Aug 03 '16 at 13:26
  • 1
    You need to show the lines causing the error. And if there is a block surrounding all these commands – jeb Aug 03 '16 at 13:27
  • Sorry for the lack of info. Updated the post. – Anonymous Aug 03 '16 at 17:02
  • Okay, everything becomes clearer now; it seems you are confusing `ErrorLevel` with the *error number specific to the `net` command*; the `ErrorLevel` is used within command prompt `cmd` and is not a real error code where each number identifies a specific error; many internal commands set `ErrorLevel` to `1` in case of errors, not distinguishing what actually happened; the only thing you can say for sure: an `ErrorLevel` of `0` means there was no error; *but* many commands do not touch `ErrorLevel` at all, some others just set it in case of errors but do not reset it in case of success... – aschipfl Aug 03 '16 at 17:54
  • ...any returned error messages are not connected to `ErrorLevel`... **Anyway**, I assume you want to capture the error message of a command, is that true? this can be done with a `for /F` loop, provided that the command uses the proper stream _STDERR_; I will update my answer accordingly, but I need some time to do that... – aschipfl Aug 03 '16 at 17:58

1 Answers1

1

The error numbers used by net helpmsg and ErrorLevel are totally different things; net helpmsg only understands error numbers returned by the net command.

What you are trying to accomplish is the following, I think (take a look at the explanatory remarks):

rem /* Test whether `java` is installed and can be found: */

rem // simply try to run the `java` executable:
java -version 2> nul
if ErrorLevel 1 echo ErrorLevel is set to %ErrorLevel% [9009 if not found, 0 otherwise].

rem // or use the `where` command to find the `java` executable:
where java > nul 2> nul
if ErrorLevel 1 echo ErrorLevel is set to %ErrorLevel% [1 if not found, 0 otherwise].

if ErrorLevel 1 goto :EOF

rem /* Thest whether the Java script `name.jar` exists: */

rem // simply try to run the Java script:
java name.jar -commands 2> nul
if ErrorLevel 1 echo ErrorLevel is set to %ErrorLevel% [1 if not found, or, if the script exists, its `ErrorLevel`].

rem // or use the `where` command to find the `java` executable:
where name.jar > nul 2> nul
if ErrorLevel 1 echo ErrorLevel is set to %ErrorLevel% [1 if not found, 0 otherwise].

rem /* (the `> nul` suffix suppresses output messages,
rem     the `2> nul` suffix suppresses error messages) */


rem /* To capture the error message returned by any command, do this: */
for /F delims^=^ eol^= %%E in ('
    command 2^>^&1 ^> nul
') do (
    echo This is the error message: "%%E"
)

Since the question post has been updated and clarified, the following does not cover the problem. The true solution can be found above. I keep the former answer here for reference.
This is for the case you want to extract the error number returned by the net command, which is specific to this command and has got nothing to to with the ErrorLevel:

The error number of the net command and the ErrorLevel are totally different things.

Supposing you try to execute net view, it might fail with this error message, for instance:

System error 6118 has occurred.

The list of servers for this workgroup is not currently available

The resulting ErrorLevel is 2 though.
So executing net helpmsg %ErrorLevel% does not make any sense.

In contrast, net helpmsg 6118 does, which returns the following string:

The list of servers for this workgroup is not currently available

To capture the error number of the net command, use a for /F loop and take the line of the first iteration only by an if defined condition (using command net view in this example):

set "ERRNUM=" & set "ERRTXT="
for /F "delims=" %%M in ('2^>^&1 ^> nul net view') do (
    rem Note that the `token` option is language-dependent:
    for /F "tokens=3" %%N in ("%%M") do (
        if not defined ERRNUM set "ERRNUM=%%N"
    )
    set "ERRTXT=%%M"
)

This would capture the error number 6118 of the above example and store it in variable ERRNUM. The last line of the error output (that I consider as the error message) is stored in variable ERRTXT.

To retrieve the error message later from a given error number stored in ERRNUM, you could use this:

set /A "ERRNUM+=0"
if %ERRNUM% NEQ 0 (
    for /F "delims=" %%M in ('net helpmsg %ERRNUM%') do (
        set "ERRTXT=%%M"
    )
)

Side Note: Yes, I would remove the quotes if %ErrorLevel% NEQ 0 to do a numeric comparison.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • I am sorry for a late reply, I was trying to understand the code but, i cant really. If you could compile it with my code that be much more helpful as I could understand more about this. – Anonymous Aug 03 '16 at 14:54
  • Please show the code portion that is returning the error you mentioned in your question... – aschipfl Aug 03 '16 at 15:29
  • 'If "%Errorlevel%" NEQ "0" ( set "ERRNUM=" & set "ERRTXT=" for /F "delims=" %%M in ('2^>^&1 ^> num net view') do ( rem Note that the `token` option is language-dependent: for /F "tokens=3" %%N in ("%%M") do ( if not defined ERRNUM set "ERRNUM=%%N" ) set "ERRTXT=%%M" )) echo %ERRNUM% %ERRTXT% pause' – Anonymous Aug 03 '16 at 15:33
  • No, I meant the original code that causes the error (Java is not recognized), by [editing](http://stackoverflow.com/posts/38744649/edit) your question; code in comments is hardly readable... – aschipfl Aug 03 '16 at 15:47
  • Sorry about that, updated the post. Let me know if you need any more info. – Anonymous Aug 03 '16 at 15:57
  • From what i get maybe i have to run the "java" command inside the /f loop? – Anonymous Aug 03 '16 at 16:55