1

I wrote the following .bat script

@echo off
@rem set variables
set CURRENT_DIR=%cd%
set SCRIPT_DIR=%~dp0%
set Program Files=%Program Files:Program Files=Progra~1%

:input
set INPUT=
echo Following are the installation choices you have:
echo        1. Choice1
echo        2. Choice2
echo        3. Choice3

set /P INPUT=Please select any of the above choice (1to 3) : %=%
echo input is %INPUT%
if "%INPUT%"=="" goto input

if "%INPUT%"=="1" (
    echo Choice 1 selected
    goto QUITNOW
)

if "%INPUT%"=="2" (
echo Choice 2 selected
set /p INPUT_1=Please enter input 1: %=%
echo input_1 is  %INPUT_1%
goto QUITNOW
)

if "%INPUT%"=="3" (
echo Choice 3 selected
set /p input_3= Please enter input_3: %=%
echo %input_3%
)

:QUITNOW
cd %CURRENT_DIR%
@echo on

At first when it ask me to select the choice and i entered 2,it set the INPUT variable to 2 and it goes in second if block. In if block again it ask for the input (INPUT_1),i do enter the input word but looks like this time its not setting the INPUT_1 variable.Do anyone have any idea where am i doing wrong. Following the the output of the script with my inputs

C:\Documents and Settings\Administrator\Desktop>scriptA.bat
Following are the installation choices you have:
        1. Choice1
        2. Choice2
        3. Choice3
Please select any of the above choice (1to 3) : 2
input is 2
Choice 2 selected
Please enter input 1: failingHere
input_1 is

C:\Documents and Settings\Administrator\Desktop>

So if you observe its not printing the "failingHere" input. Please help and let me know if you need any further information regarding this.

3 Answers 3

4

Your problem lies here:

if "%INPUT%"=="2" (
    echo Choice 2 selected
    set /p INPUT_1=Please enter input 1: %=%
    echo input_1 is  %INPUT_1%
    goto QUITNOW
)

This entire block is evaluated (for environment variables) before any of it is run. That means %INPUT_1% is evaluated before being set.

What you need to do is put:

setlocal enableextensions enabledelayedexpansion

at the top of your script (and endlocal before exiting) and use the delayed expansion symbols thus:

if "%INPUT%"=="2" (
    echo Choice 2 selected
    set /p INPUT_1=Please enter input 1: %=%
    echo input_1 is  !INPUT_1!
    goto QUITNOW
)

You can see this in action with the following script:

@setlocal enableextensions enabledelayedexpansion
@echo off
set xx=1
if %xx%==1 (
    set xx=2
    echo %xx%
    echo !xx!
)
endlocal

which outputs:

1
2

because %xx% is evaluated at the time the entire if statement is read while !xx! is evaluated when that line is executed.

Sign up to request clarification or add additional context in comments.

Comments

0

You used the ms-dos/dos-batch tags, but I don't think set /p is already available in MS-DOS. I thought it to be added to the Windows command prompt (cmd). If not, it would have made my batch-based text adventure a lot easier if I had known it back then. :)

Comments

0

You're using /p and not /P. Change that and will works.

if "%INPUT%"=="2" (
echo Choice 2 selected
set /P INPUT_1=Please enter input 1: %=%
echo input_1 is  %INPUT_1%
goto QUITNOW
)

2 Comments

I tried that but its same. Just FYI i am running this from cmd,but i hope that won't cause be an issue.
You're right. That is not the problem, sorry. Paxdiablo are right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.