batch-filebatch-choice

Batch file choice loop on unexpected input


So I have this choice:

echo.
echo  All done. What would you like to do next?
echo  1. Open output folder
echo  2. Exit
echo.

set /p Choose=" Select 1 or 2 and press Enter: "

if '%Choose%'==1 goto show
if '%Choose%'==2 goto end

The problem is, this script detect any other choice other than 2 as 1, which is not how I want it. Instead, I want it to loop back to choice on unexpected input. The closest thing the internet told me is:

if '%Choose%'NEQ 1 if '%Choose%'NEQ 2 goto choice

But this goes to choice on ANY input, even 1 or 2.


Solution

  • Both sides of an if must be identical for == to become true. You've single-quoted the first, so you must single-quote the second. Actually, double-quote " both sides for safety. If both if statements fail, then the choice made is neither, so goto your first echo. Speaking of choice - you should look that up as another way to get the job done. Plenty of examples on SO - just use the search box in the top line