Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account



Photo

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

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
druiddk

druiddk

    Member

  • Member
  • PipPip
  • 215 posts
  • Joined 30-November 05
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, 10 February 2006 - 10:15 AM.

Please rate my post :)


How to remove advertisement from MSFN

#2
Delprat

Delprat

    Poll: Why are you reading this ?

  • Member
  • PipPipPip
  • 484 posts
  • Joined 18-May 05
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

KISSes from France !

Will you take part in the microwave oven contest on msfn.org ?

The best game ever created is open-source and cross-platform. Get it NOW !

#3
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,073 posts
  • Joined 13-October 04
<Edit>
Removed original message due to the fact I didn't bother reading before I replied!
</Edit>

Edited by Yzöwl, 11 February 2006 - 05:55 AM.


#4
druiddk

druiddk

    Member

  • Member
  • PipPip
  • 215 posts
  • Joined 30-November 05

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!
Please rate my post :)

#5
Delprat

Delprat

    Poll: Why are you reading this ?

  • Member
  • PipPipPip
  • 484 posts
  • Joined 18-May 05

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.

KISSes from France !

Will you take part in the microwave oven contest on msfn.org ?

The best game ever created is open-source and cross-platform. Get it NOW !

#6
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,073 posts
  • Joined 13-October 04
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.


#7
osirisgothra

osirisgothra
  • Member
  • 1 posts
  • Joined 01-January 13
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...