0

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.

  • 1
    [`if "%choose%"=="1"`](https://stackoverflow.com/a/45690924/2861476) – MC ND Aug 17 '17 at 15:13
  • Use the command `choice` instead of `set /P` and you don't have this problem anymore. Run in a command prompt window `choice /?` for help. – Mofi Aug 17 '17 at 15:13
  • 1
    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 – Magoo Aug 17 '17 at 15:15
  • @Mofi That uses `ERRORLEVEL` which is used somewhere else in the script. I don't know batch script good enough to mess with that. – George Hovhannisian Aug 17 '17 at 15:21
  • @Magoo that did the trick, thank you. Can you post it as an answer so I can set it as such? Unless, this is gonna be marked as duplicate. Also, this is an already existing script that I'm trying to customize for myself, so I didn't come up with `set /p`. That's what the author of the script chose. – George Hovhannisian Aug 17 '17 at 15:23
  • `errorlevel` is updated by nearly any command executed within the batch file. It holds the exit code of the previously executed command or executable. So it only matters which command/executable was executed just before the line containing `if errorlevel` and not what was executed 20 lines above. – Mofi Aug 17 '17 at 15:34

2 Answers2

1

Use this code any the user can't press anything else than 1 or 2 (or Ctrl+C to break batch file execution) with immediate continuation of script processing after having hit key 1 or 2.

@echo off
echo/
echo  All done. What would you like to do next?
echo/
echo  1. Open output folder
echo  2. Exit
echo/
%SystemRoot%\System32\choice.exe /C 12 /N /M "Select 1 or 2: "
if errorlevel 2 goto :EOF

rem Code to run on open output folder.
echo Use selected 1.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • choice /?
  • echo /?
  • if /?
  • goto /?
  • rem /?

Read also the Microsoft support article Testing for a Specific Error Level in Batch Files why it is here enough to use only if errorlevel 2 goto :EOF as the only remaining option is that errorlevel has value 1 after execution of choice.

And read also DosTips forum topic ECHO. FAILS to give text or blank line - Instead use ECHO/ why it is better to use echo/ instead of echo. to output an empty line.

Read also:

Mofi
  • 38,783
  • 14
  • 62
  • 115
1

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

Magoo
  • 68,705
  • 7
  • 55
  • 76
  • 1
    @GeorgeHovhannisian Please note that on many keyboards pressing Shift+2 instead of just 2 results in entering `"` character. So if a user enters by mistake `"` because of Shift is hold (or CapsLock is active depending on keyboard layout) and press next RETURN, the batch file processing is exited by Windows command interpreter because of a syntax error as the result is `if """ == "1" goto show` which is an invalid command line. It is really hard to make `set /P` input fail safe against typing mistakes by the user. For simple selections from a list `choice` is definitely the better __choice__. – Mofi Aug 17 '17 at 15:31