CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2006
    Posts
    50

    [RESOLVED] batch: IF ELSE STATEMENT

    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?
    ---------
    Code:
    @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..

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: batch: IF ELSE STATEMENT

    Sorry to tell you but there is no way to change cases in just plain DOS.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: batch: IF ELSE STATEMENT

    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.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Feb 2008
    Posts
    1

    Re: [RESOLVED] batch: IF ELSE STATEMENT

    You can simulate IF ELSE statement like this:

    Code:
    @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
    Last edited by PeejAvery; February 22nd, 2008 at 08:05 AM. Reason: Added code tags.

  5. #5
    Join Date
    Mar 2008
    Posts
    1

    Re: [RESOLVED] batch: IF ELSE STATEMENT

    There is indeed IF-ELSE syntax in batch. This works for me, but you have to be sure to use it exactly as shown:

    Code:
    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.
    Last edited by Quimbly; March 4th, 2008 at 12:55 PM.

  6. #6
    Join Date
    Jun 2009
    Posts
    1

    Re: [RESOLVED] batch: IF ELSE STATEMENT

    A way to use an IF ELSE IF statement is simply nest the ELSE IF

    IF <arg> (

    ....


    ) ELSE (

    IF (

    ......

    )

    ......

    )

  7. #7
    Join Date
    May 2002
    Posts
    10,943

    Re: [RESOLVED] batch: IF ELSE STATEMENT

    Welcome to the forums IT Man. Please remember to keep your posts relevant. This thread is over a year old.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured