user3037449
user3037449

Reputation: 25

CMD IF statements

I want to check whether or not variable h="yes" or h="no" and my code is obviously incorrect. how could i fix it?

@echo what is your statement (YES/NO)?

 set /p h=

 if %h% EQ 'yes' goto YES

 if %h% EQ 'no' goto NO

:YES

@echo you chose YES

 pause > nul

goto END


:NO

@echo you chose NO

 pause > nul

:END

Upvotes: 0

Views: 133

Answers (2)

Stephan
Stephan

Reputation: 56208

just a little chages are neccessary:

@echo off 
echo what is your statement (YES/NO)?
set /p h=
if /i '%h%' == 'yes' goto YES
if /i '%h%' == 'no' goto NO
echo wrong input
goto END

:YES
echo you chose YES
pause > nul
goto END

:NO
echo you chose NO
 pause > nul
:END
  • the correct syntax for "is equal to" is: EQU or ==
  • if /i makes it case-insensitive, so you can enter yes, YES, Yes or yEs
  • I added two lines for wrong input

Upvotes: 1

Leptonator
Leptonator

Reputation: 3519

I have been asked to whittle my response down.

I think this what you are looking for which is something like:

@ECHO OFF
CLS

CHOICE /C YN /M "DO YOU WANT TO RESTART APACHE TOMCAT WEB SERVICE?"
IF %ERRORLEVEL% == 2 GOTO END

ECHO DO OTHER STUFF HERE

:END
ECHO YOU PRESSED N OR SCRIPT HAS FINISHED
EXIT

Upvotes: 0

Related Questions