druiddk Posted February 10, 2006 Posted February 10, 2006 (edited) Hi!Im getting desperate here!Im trying to do:@echo offif "%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 offset choice=set /p choice=Do you want to continue? [y/n]echo You entered: %choice%pauseThen it works perfectly... PLEASE IDEAS ??!?! Edited February 10, 2006 by druiddk
Delprat Posted February 10, 2006 Posted February 10, 2006 What a funny problem Try this one :@echo offsetlocal enableextensions enabledelayedexpansionset Username=Testset choice=nothingif "%Username%"=="Test" (set /p choice=Do you want to continue? [y/n]echo You entered: !choice!pause)The truth is inline...bye 1
Yzöwl Posted February 10, 2006 Posted February 10, 2006 (edited) <Edit>Removed original message due to the fact I didn't bother reading before I replied!</Edit> Edited February 11, 2006 by Yzöwl
druiddk Posted February 10, 2006 Author Posted February 10, 2006 What a funny problem Try this one :@echo offsetlocal enableextensions enabledelayedexpansionset Username=Testset choice=nothingif "%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!
Delprat Posted February 11, 2006 Posted February 11, 2006 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 valueNow you can stop reading.
Yzöwl Posted February 11, 2006 Posted February 11, 2006 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 offif "%Username%" equ "Test" call :istestgoto :eof:istest(set choice=)(set /p choice= Do you want to continue? [y/n] )echo/ You entered: %choice%pausegoto :eofHowever, 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 nwhat if they entered Y or N instead of y or nwhat if they entered Harry Potter instead of y or nwhat if they entered nothing at all and just hit the enter keyetc.
osirisgothra Posted January 1, 2013 Posted January 1, 2013 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...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now