Yore
Yore

Reputation: 430

Windows Batch: Chained IF ELSE with Prompts

I'm trying to write a windows batch script to do some work for me. Here is the code:

@echo off

cls

set /P AA="Is this information correct (Y/[N]) ? "
if /I "%AA%" == "Y" (

    echo Setting up %DATE% %TIME% ...

    echo Copying stuff to the places ...

    set /P BB="Overwrite (Y/[N]) ? "
    if /I "%BB%" == "Y" (

        echo Executing xxx ...

    ) else echo NOPE1 [%BB%]

    set /P CC="Overwrite (Y/[N]) ? "
    if /I "%CC%" == "Y" (

        echo Executing xxx ...

    ) else echo NOPE2 [%CC%]

    echo All set !

) else echo Setup aborted [%AA%] !

pause

The 2nd and 3rd answers are always empty, and when I remove the quotes from the prompt it blames the `?' character. What is wrong with the code ?

Thanks.

Upvotes: 3

Views: 70

Answers (1)

Yore
Yore

Reputation: 430

This is kind of nonsense, but anyways, due to the 'Delayed Expansion' I modified the code to:

@echo off
Setlocal EnableDelayedExpansion

cls

set /P AA="Is this information correct (Y/[N]) ? "
if /I "%AA%" == "Y" (

    echo Setting up %DATE% %TIME% ...

    echo Copying stuff to the places ...

    set /P BB="Overwrite (Y/[N]) ? "
    if /I "!BB!" == "Y" (

        echo Executing xxx ...

    ) else echo NOPE1 [!BB!]

    set /P CC="Overwrite (Y/[N]) ? "
    if /I "!CC!" == "Y" (

        echo Executing xxx ...

    ) else echo NOPE2 [!CC!]

    echo All set !

) else echo Setup aborted [%AA%] !

pause

And it's working ! Thanks npocmaka and Stephan

Upvotes: 3

Related Questions