Can someone help me with my batch. I’m trying to have an escape at the beginning of my batch but it’s not working. I’m sure it’s simple but I’m not a regular user of this type of programming.
cls
@echo off
set usr_out=y
set /p usr_out=Press [N] to cancel:
if NOT %usr_out% == Y goto myend
if NOT %usr_out% == y goto myend
echo in
pause
exit
:myend
echo out
pause
EDIT: Sorry, I forgot to detail that the code above only goes to the echo out line when I don’t enter a value.
You set the default value of
%usr_out%to a lowercasey:But then check if the variable is unequal to an uppercase
Y:Since
yandYare indeed unequal, your script correctly jumps to:myendat this point.You need to fix your logic here. If you want to skip over a code block when all of a given number of conditions are not met you have to use something like this in batch:
However, in your particular case you can use a simpler approach, because you only need a case-insensitive check for one single letter:
On a different note: instead of a plain
exitI’d recommend using eithergoto :eof(jump to the end of the script) orexit /b(exit the batch script without terminatingcmd.exe). Otherwise running the script manually in a command prompt might inadvertently terminate the command prompt window.