Description: |
This simple menu framework parses itself for batch labels of certain signature and lists them as menu items. The self-parsing feature
makes the menu generic. New menu items can be inserted by adding new function blocks without changing the menu infrastructure.
Features:
- simple and well structured
- easy to enhance
- easy to maintain
|
Script: |
Download: BatchMenu.bat
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36.
|
@ECHO OFF
REM.-- Prepare the Command Processor
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
:menuLOOP
echo.
echo.= Menu =================================================
echo.
for /f "tokens=1,2,* delims=_ " %%A in ('"findstr /b /c:":menu_" "%~f0""') do echo. %%B %%C
set choice=
echo.&set /p choice=Make a choice or hit ENTER to quit: ||GOTO:EOF
echo.&call:menu_%choice%
GOTO:menuLOOP
::-----------------------------------------------------------
:: menu functions follow below here
::-----------------------------------------------------------
:menu_1 Have some fun
echo.Have some fun by adding some more code right here
GOTO:EOF
:menu_2 Get a water
echo.Get a water and then add some code right here
GOTO:EOF
:menu_
:menu_T Tip
echo.It's easy to add a line separator using one or more fake labels
GOTO:EOF
:menu_C Clear Screen
cls
GOTO:EOF
|
|
Script Output: |
Script Output | = Menu =================================================
1 Have some fun
2 Get a water
T Tip
C Clear Screen
Make a choice or hit ENTER to quit:
| |
Description: |
Example - Installation Framework.
This example shows how to enhance a program with persistent settings that can be changed using a menu.
I.e.:
- Choose 1 to change the 'Install version' from 'Client' to 'Server'.
- Choose 1 again to change the 'Install version' back to 'Client'.
- Choose 2 to change the 'Size of installation' from Full to 'Regular'.
- Choose 2 again to change the 'Size of installation' to 'Mini'.
- Choose 2 again to change the 'Size of installation' back to 'Full'.
- Choose I to begin the installation. Note: this example is just a simulation and will not install anything on your PC.
Features:
- simple and well structured
- settings changeable at runtime
- settings are persistent, the batch will remember all settings from the last run
- progress indication during simulated file installation
|
Script: |
Download: BatchInstall.bat
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169. 170. 171. 172. 173. 174.
|
@ECHO OFF
REM.-- Prepare the Command Processor
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
REM.-- Version History --
REM XX.XXX YYYYMMDD Author Description
SET version=01.000-beta &rem 20051201 p.h. initial version, providing the framework
REM !! For a new version entry, copy the last entry down and modify Date, Author and Description
SET version=%version: =%
REM.-- Set the window title
SET title=%~n0
TITLE %title%
REM.--initialize the variables
set FilePersist=%~dpn0+.cmd& rem --define the filename where persistent variables get stored
set SvrCli_choice=,Server,Client,
call:setPersist SvrCli=Server
set bShowReadMe_choice=,Yes,No,
call:setPersist bShowReadMe=No
set InstSize_choice=,Full,Regular,Mini,
call:setPersist InstSize=Full
rem.--read the persistent variables from the storage
call:restorePersistentVars "%FilePersist%"
:menuLOOP
echo.
echo.= Menu =================================================
echo.
for /f "tokens=1,2,* delims=_ " %%A in ('"findstr /b /c:":menu_" "%~f0""') do echo. %%B %%C
set choice=
echo.&set /p choice=Make a choice or hit ENTER to quit: ||(
call:savePersistentVars "%FilePersist%"& rem --save the persistent variables to the storage
GOTO:EOF
)
echo.&call:menu_%choice%
GOTO:menuLOOP
::-----------------------------------------------------------
:: menu functions follow below here
::-----------------------------------------------------------
:menu_Options:
:menu_1 Install version : '!SvrCli!' [!SvrCli_choice:~1,-1!]
call:getNextInList SvrCli "!SvrCli_choice!"
cls
GOTO:EOF
:menu_2 Size of installation : '!InstSize!' [!InstSize_choice:~1,-1!]
call:getNextInList InstSize "!InstSize_choice!"
cls
GOTO:EOF
:menu_3 Show Readme.txt when finished: '!bShowReadMe!' [!bShowReadMe_choice:~1,-1!]
call:getNextInList bShowReadMe "!bShowReadMe_choice!"
cls
GOTO:EOF
:menu_
:menu_Execute:
:menu_I Start Installation (simulation only)
set maxcnt=20
if /i "%InstSize:~0,1%"=="F" set maxcnt=11
if /i "%InstSize:~0,1%"=="R" set maxcnt=7
if /i "%InstSize:~0,1%"=="M" set maxcnt=3
echo.Simulating an installation for !maxcnt! files...
call:initProgress maxcnt
for /l %%C in (1,1,!maxcnt!) do (
echo.Pretend to install !SvrCli! file %%C.
call:sleep 1
call:doProgress
)
call:sleep 1
TITLE %title%
if /i "%bShowReadMe:~0,1%"=="Y" notepad ReadMe.txt
GOTO:EOF
:menu_C Clear Screen
cls
GOTO:EOF
::-----------------------------------------------------------
:: helper functions follow below here
::-----------------------------------------------------------
:setPersist -- to be called to initialize persistent variables
:: -- %*: set command arguments
set %*
GOTO:EOF
:getPersistentVars -- returns a comma separated list of persistent variables
:: -- %~1: reference to return variable
SETLOCAL
set retlist=
set parse=findstr /i /c:"call:setPersist" "%~f0%"^|find /v "ButNotThisLine"
for /f "tokens=2 delims== " %%a in ('"%parse%"') do (set retlist=!retlist!%%a,)
( ENDLOCAL & REM RETURN VALUES
IF "%~1" NEQ "" SET %~1=%retlist%
)
GOTO:EOF
:savePersistentVars -- Save values of persistent variables into a file
:: -- %~1: file name
SETLOCAL
echo.>"%~1"
call :getPersistentVars persvars
for %%a in (%persvars%) do (echo.SET %%a=!%%a!>>"%~1")
GOTO:EOF
:restorePersistentVars -- Restore the values of the persistent variables
:: -- %~1: batch file name to restore from
if exist "%FilePersist%" call "%FilePersist%"
GOTO:EOF
:getNextInList -- return next value in list
:: -- %~1 - in/out ref to current value, returns new value
:: -- %~2 - in choice list, must start with delimiter which must not be '@'
SETLOCAL
set lst=%~2& rem.-- get the choice list
if "%lst:~0,1%" NEQ "%lst:~-1%" echo.ERROR Choice list must start and end with the delimiter&GOTO:EOF
set dlm=%lst:~-1%& rem.-- extract the delimiter used
set old=!%~1!& rem.-- get the current value
set fst=&for /f "delims=%dlm%" %%a in ("%lst%") do set fst=%%a&rem.--get the first entry
rem.-- replace the current value with a @, append the first value
set lll=!lst:%dlm%%old%%dlm%=%dlm%@%dlm%!%fst%%dlm%
rem.-- get the string after the @
for /f "tokens=2 delims=@" %%a in ("%lll%") do set lll=%%a
rem.-- extract the next value
for /f "delims=%dlm%" %%a in ("%lll%") do set new=%%a
( ENDLOCAL & REM RETURN VALUES
IF "%~1" NEQ "" (SET %~1=%new%) ELSE (echo.%new%)
)
GOTO:EOF
:initProgress -- initialize an internal progress counter and display the progress in percent
:: -- %~1: in - progress counter maximum, equal to 100 percent
:: -- %~2: in - title string formatter, default is '[P] completed.'
set /a ProgressCnt=-1
set /a ProgressMax=%~1
set ProgressFormat=%~2
if "%ProgressFormat%"=="" set ProgressFormat=[PPPP]
set ProgressFormat=!ProgressFormat:[PPPP]=[P] completed.!
call :doProgress
GOTO:EOF
:doProgress -- display the next progress tick
set /a ProgressCnt+=1
SETLOCAL
set /a per=100*ProgressCnt/ProgressMax
set per=!per!%%
title %ProgressFormat:[P]=!per!%
GOTO:EOF
FOR /l %%a in (%~1,-1,1) do (ping -n 2 -w 1 127.0.0.1>NUL)
goto :eof
|
|
Script Output: |
Script Output | = Menu =================================================
Options:
1 Install version : 'Client' [Server,Client]
2 Size of installation : 'Full' [Full,Regular,Mini]
3 Show Readme.txt when finished: 'No' [Yes,No]
Execute:
I Start Installation (simulation only)
C Clear Screen
Make a choice or hit ENTER to quit:
| |