1

New to the site and to scripting in general. I am aware that you can create user inputs like this:

set input=
set /p input=[y/n]: %=%

then you can ask using "if" to see if they are certain responses. Example:

if "%input%"=="y" goto a:

and check if it isn't said input by saying

if "%input%" NEQ "y" goto b:

Because I'm uneducated, I don't know how to accomplish this, but I want to be able to determine if the input was an enter keystroke. I have tried to accomplish this by using

if "%input%"==[ENTER] goto a:

and

if "%input%"==enter

and every method I could think of to determine if the keystroke was an [ENTER]. I am probably just stupid, but is there a way to do this? Thanks.

Monacraft
  • 6,510
  • 2
  • 17
  • 29
Orbian
  • 11
  • 2
  • 3
    If they just hit enter the string will be blank, provided that it was blank before the set command: `if "%input%"==""` – Gary Jun 13 '15 at 23:58
  • 1
    You should realize that the ENTER key is _always_ pressed in order to end a `set /P` command, so your entire description is confusing. I suggest you to change the topic title to "How to detect an empty input in SET /P command" and modify the description accordingly... – Aacini Jun 14 '15 at 00:04
  • 1
    http://stackoverflow.com/questions/27489804/set-p-empty-answer-crash – Aacini Jun 14 '15 at 00:42

2 Answers2

5

There are at least two ways to do that: check the value or check the operation

set /p retrieves the typed input storing it inside a variable, but when there is no input, just an Enter press, there is no storage operation on the variable so it keeps its previous value.

The usual way to test the value is to clear the variable contents, use the set /p and test if the variable is defined, that is, it has content

set "var="
set /p "var=[y/n]?"
if not defined var goto :noUserInput

We can also test the operation itself. In batch files most of the commands set to some value the internal errorlevel variable indicating the sucess or failure of the execution of the command.

In the case of the set /p it will set errorlevel to 0 if data has been retrieved and will set it to 1 if there is no input. Then the if errorlevel construct can be used to determine if there was input

set /p "var=[y/n]?"
if errorlevel 1 goto :noUserInput

As Aacini points, this can fail. If the batch file has .bat extension a set /p that reads data will not reset (set to 0) a previous errorlevel value, so it is necessary to first execute some command to set the errorlevel to 0.

ver > nul
set /p "var=[y/n]?"
if errorlevel 1 goto :noUserInput

If the batch file is saved with .cmd extension this is not needed. In this case the set /p will set the errorlevel to 0 when it reads data.

Also conditional execution operators (&& and ||) can be used. && executes the next command if the previous one was sucessful. || will execute the next command if the previous one failed.

set /p "var=[y/n]?" || goto :noUserInput
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Just a detail here: when `set /P` command reads input it does _NOT_ modify the previous value of errorlevel, so you need to be sure that the errorlevel is zero before the `set /P`. A simple way of do that is with VER command: `ver > NUL` `set /P var=` `if errorlevel 1 goto noInput` – Aacini Jun 14 '15 at 17:07
  • @Aacini, I used to think so, but before posting the answer I tested it (XP SP3, what I had at hand) from a batch file (`echo test | find "x"`, `set /p data=`, `echo %errorlevel%`) and it works as indicated in the answer. BUT, as you point, from command line `set /p` does not clear (set to 0) the errorlevel. – MC ND Jun 14 '15 at 19:43
  • @Aacini, well to be precise, I tested it from a batch file with `.cmd` instance. If executed from a file with `.bat` extension then `set /p` does not reset the `errorlevel`. In this scenario you are right. – MC ND Jun 14 '15 at 19:56
  • @Aacini, now included in the answer. Thank you. – MC ND Jun 14 '15 at 20:01
  • Yes: if the Batch file have `.bat` extension the errorlevel is _not_ changed. It is changed when the Batch file have `.cmd` extension! Tested on Win 8.1 – Aacini Jun 14 '15 at 20:02
1
If not defined input ...

detects whether input has an assigned value.

Coding

set "input="
set /p "input=[y/n]: "
if not defined input echo Enter alone detected

should detect the enter key alone being operated.

The syntax set "var=string" ensures that trailing spaces on the line are not included in the value assigned (they're hard to see).

Note that a response of simply Enter will leave the target variable unchanged; it will not assign nothing to the variable. For this reason, it is wise to ensure that the variable is cleared before a manual entry.

Magoo
  • 77,302
  • 8
  • 62
  • 84