Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for secondchoice.bat

(view source code of secondchoice.bat as plain text)

  1. @ECHO OFF
  2. IF "%~4"=="" GOTO Syntax
  3. ECHO.%* | FIND "/?" >NUL && GOTO Syntax
  4.  
  5. SETLOCAL ENABLEDELAYEDEXPANSION
  6. SET SecondChoice=
  7. SET Message=
  8. SET Default=
  9. SET Index=-1
  10. FOR %%A IN (%*) DO (
  11. 	IF !Index! EQU -1 (
  12. 		SET Message=%%~A
  13. 	) ELSE (
  14. 		IF !Index! EQU 0 (
  15. 			SET Default=%%~A
  16. 		) ELSE (
  17. 			SET Selected.!Index!=%%~A
  18. 		)
  19. 	)
  20. 	SET /A Index += 1
  21. )
  22. :Again
  23. SET Selected=%Default%
  24. CLS
  25. ECHO.
  26. ECHO.%Message%
  27. FOR /L %%A IN (1,1,3) DO (
  28. 	ECHO.
  29. 	IF "%%~A"=="%Selected%" (
  30. 		ECHO %%A. !Selected.%%~A! ^(default^)
  31. 	) ELSE (
  32. 		ECHO %%A. !Selected.%%~A!
  33. 	)
  34. )
  35. ECHO.
  36. SET /P Selected=
  37. IF "!Selected.%Selected%!"=="" GOTO :Again
  38. SET SecondChoice=!Selected.%Selected%!
  39. ENDLOCAL & SET SecondChoice=%SecondChoice%& EXIT /B %Selected%
  40. GOTO:EOF
  41.  
  42.  
  43. :Syntax
  44. ECHO.
  45. ECHO SecondChoice.bat,  Version 1.00 for Windows XP and later
  46. ECHO Present a list of choices, and wait for the user to either pick
  47. ECHO a choice by number or to press Enter for the default choice
  48. ECHO.
  49. ECHO Usage:    SecondChoice "prompt" default "first" "second" [ "third" [ ... ] ]
  50. ECHO.
  51. ECHO Where:    "prompt"     is the message to be displayed above the list of choices
  52. ECHO           default      is the index of the default choice in the list
  53. ECHO           "first" ...  are the choices to be listed (minimal 2, maximal 8)
  54. ECHO.
  55. ECHO Notes:    The list of choices will be presented until a valid choice is made.
  56. ECHO           The text of the selected choice is saved in variable %%SecondChoice%%
  57. ECHO           The index of the selected choice is returned as "errorlevel".
  58. ECHO           In case of ^(command line^) errors, the returned "errorlevel" is 0.
  59. ECHO.
  60. ECHO Example:  SecondChoice "Select speed:" 2 slow normal fast
  61. ECHO.
  62. ECHO           Select speed:
  63. ECHO           1. slow
  64. ECHO           2. normal (default)
  65. ECHO           3. fast
  66. ECHO.
  67. ECHO Written by Rob van der Woude
  68. ECHO http://www.robvanderwoude.com
  69. EXIT /B 0
  70.  

page last uploaded: 2017-08-21, 14:26