1. You are viewing our forum as a guest. For full access please Register. WindowsBBS.com is completely free, paid for by advertisers and donations.

Problem with bat file (if/else)

Discussion in 'General Discussions' started by Starsurfer, 2009/03/13.

  1. 2009/03/13
    Starsurfer

    Starsurfer Inactive Thread Starter

    Joined:
    2002/08/30
    Messages:
    17
    Likes Received:
    0
    Hi.

    when i try to use the if else statement in a batch file the else statment gets executed even when it shouldn't.
    I've echoed %1 & it displays the value typed on the command line like if i type
    lib.bat hell it echoes hell
    so when I supply %1 on the commandline the else shouldn't execute, but it still does : /
    would appreciate any feedback & help.

    Thanks

    Code:
    @echo off
    
    echo %1
    
    if defined %1 (
    set libname=%1
    ) else (
    echo Enter Library Name without an extension
    set /p libname=
    )
    
    echo libname - %libname%.lib
    pause
    exit
     
  2. 2009/03/14
    narutokage

    narutokage Inactive

    Joined:
    2009/03/13
    Messages:
    9
    Likes Received:
    0
    i am confused !! let me see again

    :)
     
    Last edited: 2009/03/14

  3. to hide this advert.

  4. 2009/03/16
    ReggieB

    ReggieB Inactive Alumni

    Joined:
    2004/05/12
    Messages:
    2,786
    Likes Received:
    2
    I think you are assuming that the command line IF is cleverer than it really is. IF will let you do one thing or another, but it won't letter you choose between blocks. You have to be more basic. You have to use GOTOs in batch commands to work with different blocks of code.

    Also, I think part of the problem is that %1 is always defined. It is a variable called %1. You are testing the existence of the variable - not whether it contains anything.

    If you test :
    Code:
    if not test == test%1
    you get the behaviour you want. By the way, if you test " " == %1 the system tests the " " against the command immediately after %1 if %1 is empty. Adding "test" to the comparison ensures there is always text to be matched.

    Try this:
    Code:
    @echo off
    
    echo %1
    
    if not test == test%1 GOTO SUCCESS
    echo Enter Library Name without an extension
    set /p libname=
    GOTO END
    :SUCCESS
    set libname=%1
    :END
    echo libname - %libname%.lib
    If you'd rather use a more sophisticated if statement, you'd be better off writing your code in vbscript, or something else.
     
  5. 2009/03/24
    Starsurfer

    Starsurfer Inactive Thread Starter

    Joined:
    2002/08/30
    Messages:
    17
    Likes Received:
    0
    Reggie, thanks for shedding some Light on the problem

    there's a prob with this too
    think i'll just look for something else to script with like you said
     
    Last edited: 2009/03/24
  6. 2009/03/25
    ReggieB

    ReggieB Inactive Alumni

    Joined:
    2004/05/12
    Messages:
    2,786
    Likes Received:
    2
    Of course if you really want a pleasurable coding experience, you code try Ruby! THE most splendid coding language in god's great universe!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.