Twml
Twml

Reputation: 441

Asking for user input in batch

I need batch code that if a user types yes goto :yes if type anything else goto :no

this is my code

@echo off
title CMD

:main
CLS
set /p input= 
if %input%==yes goto yes
else goto no
goto main

:yes
cls
echo you typed yes
pause>nul
exit

:no
cls
echo you typed something other then yes
pause>nul
exit

But it doesn't work, how can i get this to work? So far if you type yes it goes to :yes But if you type something else it just goes to a blank screen.

Upvotes: 0

Views: 204

Answers (2)

KaiLando
KaiLando

Reputation: 109

You can do:

@echo off
title CMD

:main
CLS
set /p input= 
if %input%==yes goto yes
else(if(%input%==no)):goto no
goto main

:yes
cls
echo you typed yes
pause>nul
exit

:no
cls
echo you typed no
pause>nul
exit

The main part of it is set /p input= .

Upvotes: 0

aphoria
aphoria

Reputation: 20209

You need some () in your IF/ELSE.

Try this:

@echo off
title CMD

:main
CLS
set /p input= 
if %input%==yes (
    goto yes
) else (
    goto no
)
goto main

:yes
cls
echo you typed yes
pause>nul
exit

:no
cls
echo you typed something other then yes
pause>nul
exit

Upvotes: 1

Related Questions