0

I can't find anything on how to use errorlevel in batch and I'm super confused. I've tried using it in the form of a variable but it doesn't work. Anything helps!

@ECHO off
set charisma="1"
set intelligence="1"
set strength="1"
set speed="1"
set agility="1"
echo Skill Levels
echo charisma %charisma%
echo intelligence %intelligence%
echo strength %strength%
echo speed %speed%
echo agility %agility%
set /p lvlup="Lvl up a skill? (Y/N)"

if %errorlevel% == 1 echo test
  • 1
    It's absolutely a variable. Please edit your question to show some code that you've tried that didn't work. (I suspect I'll be voting to close this as a duplicate of https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected but I want to confirm that's the problem first.) – SomethingDark Oct 18 '22 at 15:22
  • 2
    If you don't show us your code, and put the error level into context, then we cannot help you with it, without guessing. – Compo Oct 18 '22 at 15:23
  • 2
    You are simply using the wrong command, `set /p` is not appropriate for your task. Change it to ```%SystemRoot%\System32\choice.exe /M "Lvl up a skill"```, then change your `if` command to ```If ErrorLevel 2 Echo test```. – Compo Oct 18 '22 at 18:18
  • Be very careful with `errorlevel`. It is set by the system to a value depending on the result of an instruction, **however** if you assign a value to it (`set errorlevel=myvalue`) then the user-set value overrides the system-set value. – Magoo Oct 18 '22 at 20:46

2 Answers2

0

It's highly unrecommendable to set the value of errorlevel manually so in this context it is not exactly a variable. To set the errorlevel on exiting the batch file use e.g. exit /b 5 (or exit 5 which will exit the whole cmd session). If you set it manually it will overwrite the 'real' value and this can cause some errors.

To check the value of errorlevel you can use:

if %errorlevel% == 5 (
  echo some command
)

Or

if errorlevel 5 (
  echo some command
)

Though in the second case it will enter the if condition also if the value of errorlevel is bigger than 5. The good thing about the second approach is that it will work also when the %errorlevel% variable is overwritten.

The command prompt also sets the variables %=ExitCode% which will store the errorlevel converted to hex. And its more difficult to be overwritten as it starts with =. Also %=ExitCodeAscii% wich will store the errorlevel as asci i character if the errorlevel is bigger than 32.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
0

I believe that this code would fix my previous question.

set /p choice="enter a number 1 or 2: "
if %errorlevel% == 1 (
    echo some command
)
  • 1
    I believe that one day, I'll meet the lady of my dreams - also that we'll both be disappointed. – Magoo Jan 05 '23 at 05:50
  • You are supposed to use the [choice](https://ss64.com/nt/choice.html) command instead of `set /p`, not to change the variable name (which doesn't help at all) – Stephan Jan 05 '23 at 07:42