How to get user input - batch?

Here is how I'd do it:

@echo off
setlocal

:again
set /p ans=Do you want to do something? (y/n) 
if /i "%ans:~0,1%" EQU "Y" (
  Echo you selected Yes.
  REM do yes stuff
) ELSE (if /i "%ans:~0,1%" EQU "N" (
     Echo you selected No. 
     REM Do no stuff 
     ) ELSE (
     Echo You need to select yes or no only.
     goto :again
     )
)

That way, you catch if they don't enter either Yes or No and catch if they enter any variation of Yes or No.


Try adding this.

set /p Input=Enter Yes or No:

I have also added a goto because if you typed something that isn't a yes or a no then it would have automatically gone to yes. Failing the below code you could add this but the code at the bottom should work.

set /P INPUT=Type input: %=%
If %INPUT%=="y" goto yes 
If %INPUT%=="n" goto no

Your code should be like this:

@echo off
echo                                      ---WARNING---
echo.
echo DO YOU WANT YOUR COMPUTER TO SHUTDOWN? (y/n)
set /p Input=Enter Yes or No:
If /I "%Input%"=="y" goto yes
goto no
:yes
shutdown /s
:no
pause

Another much simpler way to do this in two lines, without using the IF statement is this:

2 LINE EDIT:

choice /c yn /n /m "DO YOU WANT YOUR COMPUTER TO SHUTDOWN? (y/n)"
goto %ERRORLEVEL%

WHOLE CODE (WITH 2 LINE EDIT):

@echo off
echo                                      ---WARNING---
echo.
choice /c yn /n /m "DO YOU WANT YOUR COMPUTER TO SHUTDOWN? (y/n)"
goto %ERRORLEVEL%
:1
shutdown /s
:2
pause

Explanation of choice, a built in command:

This tool allows users to select one item from a list of choices and returns the index of the selected choice. The /c yn specifies the choices the user has to choose from. The /n hides the default prompt from the user. The /m gives a custom message to the user, defined inside the " ". The second line needed is the goto %ERRORLEVEL%, which sends the code to the desired : in the code. %ERRORLEVEL% (this explanation is for the choice command only. %ERRORLEVEL% can be used in MANY other ways.) returns a number between 1 and x, where x is the total number of choices. In this example, 1 will be returned when y is pressed, and 2 will be returned when n is pressed.

EDIT: You are kind of on track, but you just were using the wrong commands. Another way you could implement my fix is this:

@echo off
echo                                      ---WARNING---
echo.
choice /c yn /n /m "DO YOU WANT YOUR COMPUTER TO SHUTDOWN? (y/n)"
set INPUT=%ERRORLEVEL%
if %INPUT% EQU 1 goto yes
if %INPUT% EQU 2 goto no
:yes
shutdown /s
:no
pause

For the if command, you have the C++ syntax for equals. In batch, you use EQU for equals, NEQ for not equal, LSS for less than, LEQ for less than of equal to, GTR for greater than, and GEQ for greater than or equal to. For some more in depth information, you may want to do if /? when you open command prompt up next. It is full of sooo much information.

This also removes any id10t user errors, meaning anything the user could do to try to break it or use it wrong.

~Burn

Tags:

Batch File