DosTips.com ... for VISTA,XP,NT,2000,2003,2008
Search:
Last update:
Jan 7, 2008

<<< PREF - - 1 2 - - NEXT >>>

2. DOS Batch - Menu Example for Installation Framework

Description

This example shows how to enhance a program with persistent settings that can be changed using a menu.

I.e.:

Features:

Script Output

 DOS Script Ouput
= 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:
Download: BatchInstall.bat  

Script

  1. @ECHO OFF
  2. REM.-- Prepare the Command Processor
  3. SETLOCAL ENABLEEXTENSIONS
  4. SETLOCAL ENABLEDELAYEDEXPANSION
  5. REM.-- Version History --
  6. REM         XX.XXX           YYYYMMDD Author Description
  7. SET version=01.000-beta &rem 20051201 p.h.  initial version, providing the framework
  8. REM !! For a new version entry, copy the last entry down and modify Date, Author and Description
  9. SET version=%version: =%
  10. REM.-- Set the window title
  11. SET title=%~n0
  12. TITLE %title%
  13. REM.--initialize the variables
  14. set FilePersist=%~dpn0+.cmd&     rem --define the filename where persistent variables get stored
  15. set             SvrCli_choice=,Server,Client,
  16. call:setPersist SvrCli=Server
  17. set             bShowReadMe_choice=,Yes,No,
  18. call:setPersist bShowReadMe=No
  19. set             InstSize_choice=,Full,Regular,Mini,
  20. call:setPersist InstSize=Full
  21. rem.--read the persistent variables from the storage
  22. call:restorePersistentVars "%FilePersist%"
  23. :menuLOOP
  24. echo.
  25. echo.= Menu =================================================
  26. echo.
  27. for /f "tokens=1,2,* delims=_ " %%A in ('"findstr /b /c:":menu_" "%~f0""') do echo.  %%B  %%C
  28. set choice=
  29. echo.&set /p choice=Make a choice or hit ENTER to quit: ||(
  30.     call:savePersistentVars "%FilePersist%"&   rem --save the persistent variables to the storage
  31.     GOTO:EOF
  32. )
  33. echo.&call:menu_%choice%
  34. GOTO:menuLOOP
  35. ::-----------------------------------------------------------
  36. :: menu functions follow below here
  37. ::-----------------------------------------------------------
  38. :menu_Options:
  39. :menu_1   Install version              : '!SvrCli!' [!SvrCli_choice:~1,-1!]
  40. call:getNextInList SvrCli "!SvrCli_choice!"
  41. cls
  42. GOTO:EOF
  43. :menu_2   Size of installation         : '!InstSize!' [!InstSize_choice:~1,-1!]
  44. call:getNextInList InstSize "!InstSize_choice!"
  45. cls
  46. GOTO:EOF
  47. :menu_3   Show Readme.txt when finished: '!bShowReadMe!' [!bShowReadMe_choice:~1,-1!]
  48. call:getNextInList bShowReadMe "!bShowReadMe_choice!"
  49. cls
  50. GOTO:EOF
  51. :menu_
  52. :menu_Execute:
  53. :menu_I   Start Installation (simulation only)
  54. set maxcnt=20
  55. if /i "%InstSize:~0,1%"=="F" set maxcnt=11
  56. if /i "%InstSize:~0,1%"=="R" set maxcnt=7
  57. if /i "%InstSize:~0,1%"=="M" set maxcnt=3
  58. echo.Simulating an installation for !maxcnt! files...
  59. call:initProgress maxcnt
  60. for /l %%C in (1,1,!maxcnt!) do (
  61.     echo.Pretend to install !SvrCli! file %%C.
  62.     call:sleep 1
  63.     call:doProgress
  64. )
  65. call:sleep 1
  66. TITLE %title%
  67. if /i "%bShowReadMe:~0,1%"=="Y" notepad ReadMe.txt
  68. GOTO:EOF
  69. :menu_C   Clear Screen
  70. cls
  71. GOTO:EOF
  72. ::-----------------------------------------------------------
  73. :: helper functions follow below here
  74. ::-----------------------------------------------------------
  75. :setPersist -- to be called to initialize persistent variables
  76. ::          -- %*: set command arguments
  77. set %*
  78. GOTO:EOF
  79. :getPersistentVars -- returns a comma separated list of persistent variables
  80. ::                 -- %~1: reference to return variable
  81. SETLOCAL
  82. set retlist=
  83. set parse=findstr /i /c:"call:setPersist" "%~f0%"^|find /v "ButNotThisLine"
  84. for /f "tokens=2 delims== " %%a in ('"%parse%"') do (set retlist=!retlist!%%a,)
  85. ( ENDLOCAL & REM RETURN VALUES
  86.     IF "%~1" NEQ "" SET %~1=%retlist%
  87. )
  88. GOTO:EOF
  89. :savePersistentVars -- Save values of persistent variables into a file
  90. ::                  -- %~1: file name
  91. SETLOCAL
  92. echo.>"%~1"
  93. call :getPersistentVars persvars
  94. for %%a in (%persvars%) do (echo.SET %%a=!%%a!>>"%~1")
  95. GOTO:EOF
  96. :restorePersistentVars -- Restore the values of the persistent variables
  97. ::                     -- %~1: batch file name to restore from
  98. if exist "%FilePersist%" call "%FilePersist%"
  99. GOTO:EOF
  100. :getNextInList -- return next value in list
  101. ::             -- %~1 - in/out ref to current value, returns new value
  102. ::             -- %~2 - in     choice list, must start with delimiter which must not be '@'
  103. SETLOCAL
  104. set lst=%~2&             rem.-- get the choice list
  105. if "%lst:~0,1%" NEQ "%lst:~-1%" echo.ERROR Choice list must start and end with the delimiter&GOTO:EOF
  106. set dlm=%lst:~-1%&       rem.-- extract the delimiter used
  107. set old=!%~1!&           rem.-- get the current value
  108. set fst=&for /f "delims=%dlm%" %%a in ("%lst%") do set fst=%%a&rem.--get the first entry
  109.                          rem.-- replace the current value with a @, append the first value
  110. set lll=!lst:%dlm%%old%%dlm%=%dlm%@%dlm%!%fst%%dlm%
  111.                          rem.-- get the string after the @
  112. for /f "tokens=2 delims=@" %%a in ("%lll%") do set lll=%%a
  113.                          rem.-- extract the next value
  114. for /f "delims=%dlm%" %%a in ("%lll%") do set new=%%a
  115. ( ENDLOCAL & REM RETURN VALUES
  116.     IF "%~1" NEQ "" (SET %~1=%new%) ELSE (echo.%new%)
  117. )
  118. GOTO:EOF
  119. :initProgress -- initialize an internal progress counter and display the progress in percent
  120. ::            -- %~1: in  - progress counter maximum, equal to 100 percent
  121. ::            -- %~2: in  - title string formatter, default is '[P] completed.'
  122. set /a ProgressCnt=-1
  123. set /a ProgressMax=%~1
  124. set ProgressFormat=%~2
  125. if "%ProgressFormat%"=="" set ProgressFormat=[PPPP]
  126. set ProgressFormat=!ProgressFormat:[PPPP]=[P] completed.!
  127. call :doProgress
  128. GOTO:EOF
  129. :doProgress -- display the next progress tick
  130. set /a ProgressCnt+=1
  131. SETLOCAL
  132. set /a per=100*ProgressCnt/ProgressMax
  133. set per=!per!%%
  134. title %ProgressFormat:[P]=!per!%
  135. GOTO:EOF
  136. :sleep -– waits some seconds before returning
  137. ::     -- %~1 – in, number of seconds to wait
  138. FOR /l %%a in (%~1,-1,1) do (ping -n 2 -w 1 127.0.0.1>NUL)
  139. goto :eof