How to get the %ERRORLEVEL%

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
barnabe0057
Posts: 21
Joined: 04 Aug 2017 14:20
Location: France

How to get the %ERRORLEVEL%

#1 Post by barnabe0057 » 09 Aug 2021 11:57

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.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to get the %ERRORLEVEL%

#2 Post by aGerman » 09 Aug 2021 12:27

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

barnabe0057
Posts: 21
Joined: 04 Aug 2017 14:20
Location: France

Re: How to get the %ERRORLEVEL%

#3 Post by barnabe0057 » 09 Aug 2021 12:39

It's so simple, I blame myself for not having thought about it.
I'll do some testing.

Thank you very much Steffen.

barnabe0057
Posts: 21
Joined: 04 Aug 2017 14:20
Location: France

Re: How to get the %ERRORLEVEL%

#4 Post by barnabe0057 » 09 Aug 2021 12:49

Alles in Ordnung ! Problem solved.

Post Reply