Page 1 of 1

How to get the %ERRORLEVEL%

Posted: 09 Aug 2021 11:57
by barnabe0057
Hi everybody,

I would like to use an external command which is called Wprompt (author: Horst Schaeffer)
https://www.horstmuc.de/w32dial.htm#wprompt

This command displays a message box with buttons (Ok, Cancel, ...)
The Exit Code (Errorlevel) is 1..3 according to the selected button number.
On timeout the default button number is returned.

I manage to get this command to work in a cmd window, I get 1 or 2. On the other hand, when I use it in my script, the %ERRORLEVEL% variable is always equal to 0, impossible to get the right value.

Code: Select all

if %essais% LSS 2 (set /a essais+=1) else (
	Wbusy "Etablissement du tunnel avant connexion" /stop /sound
	Wprompt "Notification d'échec de la connexion SSH" "La connexion au serveur relais a échoué (délai maxi : 30 sec)" OkCancel 1:15 x
	echo %ERRORLEVEL%
	pause
	goto :stop
)
Could a charitable soul come to my aid?
Thanks in advance.

Re: How to get the %ERRORLEVEL%

Posted: 09 Aug 2021 12:27
by aGerman
Variables are expanded only once in a parenthesized block of command lines. You could use delayed variable expansion. However, in your case I think the IF ERRORLEVEL legacy syntax is what you want to use (because I assume you need to branch your code). Begin with the highest possible return code, like that:

Code: Select all

if errorlevel 3 (
  ::do something for errorlevel 3 or greater than 3
) else if errorlevel 2 (
  ::do something for errorlevel 2
) else (
  ::do something for errorlevel less than 2
)
Steffen

Re: How to get the %ERRORLEVEL%

Posted: 09 Aug 2021 12:39
by barnabe0057
It's so simple, I blame myself for not having thought about it.
I'll do some testing.

Thank you very much Steffen.

Re: How to get the %ERRORLEVEL%

Posted: 09 Aug 2021 12:49
by barnabe0057
Alles in Ordnung ! Problem solved.