1.

Solve : help with if-else statement?

Answer»

My else statement does not work in my batch file. It's like it never enters the else statement. Am I doing something wrong?

I would like it to keep on asking until user either enters "y" or "Y".

Here's my code:

:open_notepad
set /p choice=OPEN Notepad?
IF "%choice%"=="y" start notepad.exe
IF "%choice%"=="Y" start notepad.exe
else (GOTO open_notepad)

thanksThe syntax is this

IF EXIST filename. (
DEL filename.
) ELSE (
echo filename. missing.
)

it is important that
) ELSE (
is on its own line

However, your logic is slightly wrong - a lowercase y will start notepad, then in the next test as it ISNT uppercase Y, it will loop again.

Try this
Code: [Select]:open_notepad
set /p choice=Open Notepad?
IF "%choice%"=="y" GOTO start_notepad
IF "%choice%"=="Y" GOTO start_notepad
GOTO open_notepad

:start_notepad
start notepad.exe
However, even this isnt perfect, the user can either open notepad .... or keep entering responses until they do want to open notepad !!

Graham
this works perfectly.

thank you.Instead of having two ifs to check for Y and y, you can use the /I switch.

It tells If to IGNORE Case, eg:

Code: [Select]If /I "%VAR%"=="Y" start notepad.exe
set /p choice=Open notepad?
if "%choice%"=="y" (start notepad.exe) ELSE cls
if "%choice%"=="Y" (start notepad.exe) ELSE goto open_notepad



Discussion

No Comment Found