agent20x Posted May 7, 2008 Posted May 7, 2008 I'm trying to make a simple menu on my winpe 2.0 boot disk , but I get an error when I use "Choice" ie.CHOICE /C:AB /N > NULIF ERRORLEVEL=2 GOTO OTHERSIF ERRORLEVEL=1 GOTO 755GOTO StartIs this not possible with PE 2.0? Is there a way to do this but with different syntax?
kyor Posted May 8, 2008 Posted May 8, 2008 In DOS and Windows 9x, the CHOICE command allowed a batch file to accept single-character user input and perform different tasks based on the result. CHOICE is gone in Windows 2000 and XP, but the Command Extensions (enabled by default) enhance other commands in ways that let you work around the absence of CHOICE.The SET command's /P switch lets you prompt for user input. For example, SET /P Choice=Type the letter and press Enter will set the environment variable Choice to whatever the user enters. SET also includes new syntax that lets you set an environment variable to a substring of another. The expression SET foo=%bar:~1,6% sets the variable foo to a substring of the variable bar starting at offset 1 (the second character) for six characters. The IF command has an /I switch for case-insensitive comparisons. Used together, those features let you create a menu much as you could with CHOICE.The code below shows a simple example. This batch file first gets the user's input into the variable Choice. In case the user accidentally types more than one letter, it selects the substring consisting of only the first letter. Then it makes a case-insensitive comparison of that letter with each of the possible menu values. You can use this example as a starting point to build your own batch file menu in Windows 2000 or Windows XP.@ECHO OFFCLS:LOOPECHO A. Menu item AECHO B. Menu item BECHO C. Menu item CECHO Q. Quit:: SET /P prompts for input and sets the variable:: to whatever the user typesSET Choice=SET /P Choice=Type the letter and press Enter::: The syntax in the next line extracts the substring:: starting at 0 (the beginning) and 1 character longIF NOT '%Choice%'=='' SET Choice=%Choice:~0,1%ECHO.:: /I makes the IF comparison case-insensitiveIF /I '%Choice%'=='A' GOTO ItemAIF /I '%Choice%'=='B' GOTO ItemBIF /I '%Choice%'=='C' GOTO ItemCIF /I '%Choice%'=='Q' GOTO EndECHO "%Choice%" is not valid. Please try again.ECHO.GOTO Loop:ItemAECHO Insert commands for Item A.GOTO Again:ItemBECHO Insert commands for Item B.GOTO Again:ItemCECHO Insert commands for Item C.GOTO Again:AgainPAUSECLSGOTO Loop:EndThis batch file implements a simple menu system. It works only at the command prompt under Windows 2000, Vista, Winpe.
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now