Jump to content

[SOLVED] Using set /p inside if-then-else statement


druiddk

Recommended Posts

Hi!

Im getting desperate here!

Im trying to do:

@echo off
if "%Username%"=="Test" (
set choice=
set /p choice=Do you want to continue? [y/n]
echo You entered: %choice%
pause
)

However that does NOT work (the variable is empty).

If i take the commands out:

@echo off
set choice=
set /p choice=Do you want to continue? [y/n]
echo You entered: %choice%
pause

Then it works perfectly... PLEASE IDEAS ??!?!

Edited by druiddk
Link to comment
Share on other sites


What a funny problem :lol:

Try this one :

@echo off
setlocal enableextensions enabledelayedexpansion
set Username=Test
set choice=nothing
if "%Username%"=="Test" (
set /p choice=Do you want to continue? [y/n]
echo You entered: !choice!
pause
)

The truth is inline...

bye

Link to comment
Share on other sites

What a funny problem :lol:

Try this one :

@echo off
setlocal enableextensions enabledelayedexpansion
set Username=Test
set choice=nothing
if "%Username%"=="Test" (
set /p choice=Do you want to continue? [y/n]
echo You entered: !choice!
pause
)

I see that it works while using !choice! but not using %choice% (well using %choice% it says You entered: nothing)...

I will look into ! ! and setlocal enableextensions enabledelayedexpansion...

Thank you very much for your help!

Link to comment
Share on other sites

I see that it works while using !choice! but not using %choice% (well using %choice% it says You entered: nothing)...

I will look into ! ! and setlocal enableextensions enabledelayedexpansion...

This piece of s***, erm, of script :

if "%Username%"=="Test" (
set /p choice=Do you want to continue? [y/n]
echo You entered: !choice!
pause
)

...Is interpreted as a single line by CMD.EXE :

if "%Username%"=="Test" (set /p choice=Do you want to continue? [y/n]&echo You entered: !choice!&pause)

If you use %choice%, the variable expansion will be done before executing the full line ; so :

-> %Username% and %choice% will get their values filled in simultaneously

-> the IF test is executed, and return true (in this example)

-> the SET /P is executed and the variable CHOICE gets the inputed value

-> the ECHO displays the old value, because %Choice% has already been replaced by "nothing"

If you use !choice! (this needs the "setlocal enabledelayedexpansion"), the variable expansion will be done only when needed ; so :

-> %Username% will get its value filled in

-> the IF test is executed, and return true (in this example)

-> the SET /P is executed and the variable CHOICE gets the inputed value

-> Now, !choice! is replaced by the value of the variable CHOICE

-> the ECHO displays the new value

Now you can stop reading.

Link to comment
Share on other sites

As a follow on from Delprats comments, the problem you had would be solved by re-structuring the code, not by adding delayed expansion

@echo off
if "%Username%" equ "Test" call :istest
goto :eof

:istest
(set choice=)
(set /p choice= Do you want to continue? [y/n] )
echo/ You entered: %choice%
pause
goto :eof

However, you really should be doing some sort of checks on the user input before continuing with the script.

  • What if they entered g instead of y or n
    what if they entered Y or N instead of y or n
    what if they entered Harry Potter instead of y or n
    what if they entered nothing at all and just hit the enter key
    etc.

Link to comment
Share on other sites

  • 6 years later...

use choice.exe, or xcon.exe (or some other scripting tool)

they work much better, and do checking, etc for you. CHOICE.EXE is distributed with

almost all versions of windows (for some reason not with XP) -- newer versions like Vista, windows 7, and windows 8 as well as their server alter-egos (server 2008, 2012) all come with choice.exe and it is a lot easier to use that plus you don't have to worry about cleaning up environment variables, etc. If you go with XCON you can go even further by controlling disply of countdown/timeout, color, sound, cursor positioning, console window title, direct color palette control (windows vista or better) etc, etc...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...