Windows batch if being selected on wrong value - windows

I'm trying to use the CHOICE command in a little test batch file.
Here is the code I have:
#ECHO Off
choice /M "Is this correct"
IF ERRORLEVEL 1 echo This is correct
IF ERRORLEVEL 2 echo This is not correct
echo %errorlevel%
When I press y I get This is correct but when I press n I get This is correct and This is not correct
Why is the first option being triggered? From the echo %errorlevel% I can see the errorlevel is 2.
I am using echo here as an example, it the actual batch file I have a goto and I am getting the first goto triggered all the time.

Found on ss64
IF ERRORLEVEL n statements should be read as IF Errorlevel >= number
Meaning that it will execute all other functions that are less than or equal to ERRORLEVEL.
To avoid that, try this.
#Echo off
choice /m "Would you like to continue?"
if %ERRORLEVEL%==1 Echo Response was Y
if %ERRORLEVEL%==2 Echo Response was N
Echo %ERRORLEVEL%
pause
The %'s surrounding the ERRORLEVEL will display the value of the variable.

Related

Batch: How to Properly Use CHOICE Inside of CALL Function?

I have a rather confusing problem when using nested CALL functions and CHOICE commands inside of a batch file.
To summarize in pseudo code:
1.) Use a CHOICE command, which uses CALL :function1 when the correct option is selected
2.) :function1 uses CALL :setVar_n
3.) :setVar_n sets a list of variables, and ends with EXIT /B to return to :function1
4.) :function1 has a CHOICE command (Y/N), where Y will continue to perform operations then end with EXIT /B, and N ends with EXIT /B immediately
The issue:
The CHOICE command in :function1 always evaluates to N (the second option) regardless of input.
I don't understand why using %ERRORLEVEL% fails, while IF ERRORLEVEL works fine. I am also unsure why the use of CALL causes %ERRORLEVEL% to stop working in the first place.
I'm trying to avoid rewriting every choice command (There must be at least 50, some with 25+ options).
When it's written using %ERRORLEVEL% it fails:
::Return from setVar_n here
CHOICE /C YN /M "Continue? Y/N >"
IF %ERRORLEVEL%==2 (EXIT /B)
::function1 continues here
If I use IF ERRORLEVEL:
::Return from setVar_n here
CHOICE /C YN /M "Continue? Y/N >"
IF ERRORLEVEL 2 (EXIT /B)
::function1 continues here
It works properly. The issue is that the CHOICE problem persists even after :function1 ends. It affects all CHOICE commands in the entire file, so %ERRORLEVEL% cannot be used at all.
Can anyone shed some light on this issue?
Here's a full file code to test with, which might make more sense:
#ECHO OFF
:start
choice /c ABC
if %errorlevel%==1 (goto start)
if %errorlevel%==2 (call :function1)
if %errorlevel%==3 (goto start)
echo Function 1 completed
pause
choice /c ABC
if %errorlevel%==1 (echo 1)
if %errorlevel%==2 (echo 2)
if %errorlevel%==3 (echo 3)
pause
exit
:setVar
set /a var1=2
set /a var2=3
exit /b
:function1
echo In Function 1
call :setVar
choice /c YN /m "Continue (Y) or Finish (N)"
if %errorlevel%==2 (exit /b)
echo Still inside function 1
exit /b
Thanks to Stephan for the correct answer in the comments.
The solution is to use
set "errorlevel="
Before the choice command.

"IF ERRORLEVEL" chain sets wrong value

I am trying to use the choice command in a batch script to take the default input if the user doesn't want to stop kicking off a report. I wrote the below script but instead of waiting 10 seconds and kicking off the report, it is recursively echo-ing the first line of the code over and over until I kill the script. Is there something wrong that I am doing?
My Code:
CHOICE /C YN /N /T 10 /D N /M "Run Report Y or N?"
IF ERRORLEVEL 1 SET REPORT=RunTheReport:
IF ERRORLEVEL 2 SET REPORT=DontRunIt:
ECHO You chose to run %REPORT%
P.S: I replaced the report commands with an echo statement but it is still not working
You have found one of the few instances where the difference between .cmd and .bat is important.
The sample you posted works correctly, when you save that code to a file named script.bat.
script.bat
CHOICE /C YN /N /T 10 /D N /M "Run Report Y or N?"
IF ERRORLEVEL 1 SET REPORT=RunTheReport
IF ERRORLEVEL 2 SET REPORT=DontRunIt
ECHO You chose to run %REPORT%
When the user presses Y the errorlevel is set to 1. The first IF line matches and sets REPORT=RunTheReport. The second IF line does not match, and the end result is Run.
When the user presses N the errorlevel is set to 2. The first IF line matches and sets REPORT=RunTheReport. The second IF line matches and sets REPORT=DontRunIt. The end result is Don't Run.
In a .bat file, the IF ERRORLEVEL ladder will continue and execute every matching SET line. The last matching SET will be the one used.
If you save that same code to a file named script.cmd CMD behaves a little bit differently. One difference is that the SET command now sets ERRORLEVEL to 0 when it successfully sets a variable.
When the user presses Y the errorlevel is set to 1. The first IF line matches and sets REPORT=RunTheReport. The second IF line does not match, and the end result is Run, just like the .bat case.
When the user presses N the errorlevel is set to 2. The first IF line matches, sets REPORT=RunTheReport, and sets ERRORLEVEL to 0. The second IF line then does NOT match. The end result is also Run, which is wrong.
In a .cmd file, only the first matching SET line is run.
Therefore, if your file is named with a .cmd extension, you must reverse the order of the IF ERRORLEVEL lines, so that the correct one is the only one executed.
script.cmd
CHOICE /C YN /N /T 10 /D N /M "Run Report Y or N?"
IF ERRORLEVEL 2 SET REPORT=DontRunIt
IF ERRORLEVEL 1 SET REPORT=RunTheReport
ECHO You chose to run %REPORT%
There is an easy way to avoid this issue and make your code work in both types of file. The IF ERRORLEVEL N syntax is deprecated. It is confusing because it matches as greater-than-or-equal, rather than equal. Instead, use the newer IF %errorlevel% EQU N syntax.
script2.{bat|cmd}
CHOICE /C YN /N /T 10 /D N /M "Run Report Y or N?"
IF %ERRORLEVEL% EQU 1 SET REPORT=RunTheReport
IF %ERRORLEVEL% EQU 2 SET REPORT=DontRunIt
ECHO You chose to run %REPORT%
Save as test.bat and run this script. It works well. Edited to show different approaches possible. It runs a bit faster:
#echo off
CHOICE /C YN /N /T 10 /D N /M "Run Report Y or N?"
IF "%errorlevel%"=="2" (SET "REPORT=DontRunIt:"
) else (SET "REPORT=RunTheReport:")
ECHO You chose to run %REPORT%
timeout 5
exit /b

Using a Batch File to make simple Y/N questions. Problematic Code. HALP

This is the code I am having issues with. I need to execute the choice lines properly by sending to their respected classes (cls). All of the errors are with the ERRORLEVEL and choice scripts. This is the only main issue I have but if you can help by pointing out others then I would be thankful. Best wishes, Mystikality.
cls
#echo off
echo Welcome to the Alpha PPFI Program V0.001.
GOTO T1
:T1
echo This is a security program ran just by a simple Batch file.
GOTO T2
:T2
echo If you have an interest in our program, enter your response below.
GOTO Variable
:Variable
CHOICE /C YN /N /M "Welcome, if you would wish to continue, enter Y/N
below."
if %errorlevel%==Y GOTO Create
if %errorlevel%==N GOTO ExitWarning
:Create
CHOICE /C YN /N /M "Create a new PPFI interface? Y/N"
if %errorlevel%==Y GOTO BatchGen
if %errorlevel%==N GOTO Variable
:BatchGen
start call CreatePPFI_V0.001.bat
exit
pause
:ExitWarning
CHOICE /C YN /N /M "Are you sure you want to exit V0.001 PPFI interface
alpha? Y/N"
if %errorlevel%==Y GOTO Variable
if %errorlevel%==N GOTO exit'
You need to check errorlevel to be 1 (first choice, "Y") or 2 (second choice, "N")
For example
if %errorlevel%==1 GOTO Create
if %errorlevel%==2 GOTO ExitWarning
You need to check errorlevel as other poster says. However their method won't work. If errorlevel tests for that number OR HIGHER. So reverse their lines or do it in a position independent way.
if errorlevel=1 if not errorlevel=2 Echo Error level is 1 exactly
See choice /? and if /? for help.

Batch File not working for the given input

Following is the batch file that I have created, for any input its only showing is "NO INPUT PROVIDED" string, I tried searching on google and tried many things but non solved my problem.
#echo off
ECHO Please provide input... Valid input is :: Y/y (For changing path) or N/n (For not changing the path).
SETLOCAL
SET /p change=
if "%~1" equ "" GOTO ERROREND
if /I "%change%" equ "Y" GOTO YES
if /I "%change%" equ "N" GOTO NO
:YES
ECHO Y SELECTED.
GOTO SUCCESS
:NO
ECHO N SELECTED.
GOTO SUCCESS
:ERROREND
ECHO Input not recognized.
GOTO FAILURE
:SUCCESS
ECHO Task completed succcess fully.
pause
:FAILURE
ECHO NO INPUT PROVIDED.
pause
Help needed. Thank you for reading this question.
if "%~1" equ "" GOTO ERROREND
means 'if the first parameter to the batchfile is empty', so if you were to run this batch from the prompt as
yourbatchname something
then something is the first parameter.
You probably menat to use "%change%", not `"%~1".
Note: if not defined change is probably a better test.
Note: Simply pressing Enter when prompted by a set /p does not set the variable to empty. It leaves the value unchanged. Best to use
set "var="
set /p var=....
Note: batch simply charges on, instruction by instruction. It has no concept of 'end-of-procedure'. Consequently, if your entry is neither Y nor N (once you've got the %~1 issue resolved) batch will simply continue execution past the if statements to the next - the ECHO Y SELECTED.

CMD Script Errorlevel Misbehavior with IF statement

Expected outcome of the following script: PERMPING if user presses P, or PINGLOOP if user presses T. However, no matter what the user presses, the script echos both. Any idea what gives?
CHOICE /C:PT /N /M "Ping permanently (P) or temporarily (T) (%pingTimes% times)?"
echo %ERRORLEVEL%
IF ERRORLEVEL 1 ECHO PERMPING
IF ERRORLEVEL 2 ECHO PINGLOOP
I suspect you are not acurately reporting the results of your code. The code you have posted should print both if P is pressed, and only print PINGLOOP if T is pressed. The behavior is due to how the IF ERRORLEVEL statement works, as is explained by the help. To get help on any command, simply type HELP commandName or commandName /? from a command prompt. In your case, you could use IF /? to see the following
Performs conditional processing in batch programs.
IF [NOT] ERRORLEVEL number command
...
ERRORLEVEL number Specifies a true condition if the last program run
returned an exit code equal to or greater than the number
specified.
...
You have 2 choices to make your code work:
Test the conditions in decreasing numerical order and use the ELSE clause
CHOICE /C:PT /N /M "Ping permanently (P) or temporarily (T) (%pingTimes% times)?"
echo %ERRORLEVEL%
IF ERRORLEVEL 2 (
ECHO PINGLOOP
) ELSE IF ERRORLEVEL 1 (
ECHO PERMPING
)
or use IF %ERRORLEVEL%==N
CHOICE /C:PT /N /M "Ping permanently (P) or temporarily (T) (%pingTimes% times)?"
echo %ERRORLEVEL%
IF %ERRORLEVEL% == 1 ECHO PERMPING
IF %ERRORLEVEL% == 2 ECHO PINGLOOP
To expand on the answer above if command extensions are turned on you can also use:
if %errorlevel% equ 1 echo PERMPING
if %errorlevel% equ 2 echo PINGLOOP

Resources