Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: If Statement Strange Results  (Read 3306 times)

0 Members and 1 Guest are viewing this topic.

april

    Topic Starter


    Rookie

    If Statement Strange Results
    « on: October 08, 2015, 08:04:51 AM »
    Thank you all in advance for any help you can offer.

    If a condition of an IF statement is true, I want to execute some statements.  What I found very puzzling is when the condition is true, it apppears the statements inside the IF statement are read thru and executed but the prompt statement does not get the user input data.  If I remove the IF condition check, then the statements are executed and the prompt statement gets the user input data.

    Is there any way I can execute the statements inside an IF statement and gets the input data?

    Here are the 2 scripts, one with the IF statement and the other one without.  I turned the echo on so I can see what's running behind the scene.

    If condition is true:
    @echo on
    set x=29
    if /i %x%==29 (
    set qn=
    echo qn=%qn%=
    set /p qn=Enter qn:
    echo qn=%qn%=
    )

    Results:
    set x=29
    if /I 29 == 29 (
    set qn=
    echo qn==
    set /p qn=Enter qn:
    echo qn==
    )
    qn==
    Enter qn:abc
    qn==

    No IF clause:
    @echo on
    set x=29
    set qn=
    echo qn=%qn%=
    set /p qn=Enter qn:
    echo qn=%qn%=

    Results:
    set x=29
    set qn=
    echo qn==
    qn==
    set /p qn=Enter qn:
    Enter qn:abc
    echo qn=abc=
    qn=abc=

    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: If Statement Strange Results
    « Reply #1 on: October 08, 2015, 08:31:18 AM »
    You are inside a code block.  You need to use delayed expansion.

    april

      Topic Starter


      Rookie

      Re: If Statement Strange Results
      « Reply #2 on: October 08, 2015, 09:12:02 AM »
      Thank you very much!  That solved my problem.