Click to See Complete Forum and Search --> : [RESOLVED] batch: IF ELSE STATEMENT


vietboy505
February 23rd, 2006, 08:28 PM
IF ELSE STATEMENT

Since there is no else statement in Batch script. How do I by pass that?

I want this script will ask for a name if the name doesn't match the input.
The name will be hardcode, so the person who run it doesn't enter his/her name everything. It just goes directly to check status right away and print out the message.

Why this not working the way I want it?
This will goes through all command even though I try to put in JOHN or DAVE.

How can I covert the user input to all CAPS or the variable %NAME% to all CAPS?
---------

@ECHO OFF
SET NAME="ENTER_NAME_HERE"
GOTO :START_MENU

:START_MENU
IF %NAME% == "ENTER_NAME_HERE" GOTO :ASK_MENU
:END


:ASK_MENU
SET /P NAME="ENTER NAME: " GOTO :CHECK_MENU
:END

:CHECK_MENU
IF %NAME% == "JOHN" GOTO :JOHN_MENU
IF %NAME% == "DAVE" GOTO :DAVE_MENU
:END

:JOHN_MENU
ECHO YOUR NAME IS: %NAME% HAVE A NICE DAY!
:END

:DAVE_MENU
ECHO YOUR NAME IS: %NAME% PEACE OUT!
:END

PAUSE

---------------

thanks for the help..

PeejAvery
February 23rd, 2006, 10:11 PM
Sorry to tell you but there is no way to change cases in just plain DOS.

dglienna
February 23rd, 2006, 10:48 PM
You might want to try it in VB Script. You can use Visual Basic commands to convert the case, and use IF-THEN-ELSE statements.

kregsen
February 21st, 2008, 05:40 PM
You can simulate IF ELSE statement like this:

@echo off

set answer=one
if not "%1" == "1" (
set answer=two
if not "%1" == "2" (
goto bad_argument
)
)

echo Argument %1 is %answer%
goto end


:bad_argument
echo Bad Argument

:end

Quimbly
March 3rd, 2008, 04:40 PM
There is indeed IF-ELSE syntax in batch. This works for me, but you have to be sure to use it exactly as shown:


IF <statement> (
..
..
) ELSE (
...
...
)



I believe the parentheses must be used exactly as shown above, or the statement won't work properly.

I have not, however, found a nice way to handle ELSE-IF statements. So, you have to make big nested IF blocks:

E.g.
IF <statement> (
IF <statement> (
...
) ELSE (
...
)
) ELSE (
...
)

One annoyance I've noticed is that assignments (i.e. SET statements) made within an IF block are not realized until AFTER the IF-bock. So, in other words, you can't rely on variable assignment within an IF-block.

IT Man
June 10th, 2009, 11:12 AM
A way to use an IF ELSE IF statement is simply nest the ELSE IF

IF <arg> (

....


) ELSE (

IF (

......

)

......

)

PeejAvery
June 10th, 2009, 01:09 PM
Welcome to the forums IT Man. Please remember to keep your posts relevant. This thread is over a year old.

NeoCortex
June 8th, 2011, 11:49 AM
@IT Man

thx for the info, just what i needed to know :-)

@PeejAvery

for me it is relevant, so shut the f*** up about the time between posts.