Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get user input - batch?

Tags:

batch-file

So I want to code an optional, where the user can input y or n, shut down. And this is what I have been trying:

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

Am I along the right track even?

like image 765
RHYN0 Avatar asked Mar 07 '14 10:03

RHYN0


1 Answers

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
like image 112
09stephenb Avatar answered Sep 20 '22 15:09

09stephenb