Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ::: Multi-Use Menu :: Author - T3RRY :: Version 4.2 :: October 2020 ::
- ::: %Menu% macro - accepts up to 36 options for selection as quoted Args in list form.
- ::: parameters to call functions with via a given menu can be supplied using Substring modification as exampled.
- ::: - Multiple selection of menu items achieved using loops.
- ::: Default is to assume a menu option is a label to be called. If the label doesn't exist error is suppressed and the option is added as a quoted
- ::: string to the #Sel Variable for assessment - builds a list of selected options [ that can be deselected ] when used in a loop.
- ::: Windows 10 users will experience faster display of menus thanks to VT codes
- ::: Users not running Windows 10 are compensated with more dynamic color output from the menu.
- ::: "&&" and "||" conditional operators are used heavily to control script flow, and are used with findstr as an alternative to
- ::: If Not "!Errorlevel!" == "0" (Command)Else (other command) statements.
- @Echo off & Goto :Main
- :::::::::::::::::::::::::: [* Balances Environment stack on script completion *]
- :End [Endlocal ^& Set "_End=Y" ^& Exit /B 0]
- Color 07 & %CLOSE%
- :::::::::::::::::::::::::::::::::::::::::::::: Findstr based Colorprint function
- ::: Unsupported Chars: "<" ">" ":" "\" "/" "?" "&"
- :C_out [BG:a-f|0-9][FG:a-f|0-9] ["Quoted Strings" "to print"]
- Set "Str_=%*" & Set "_Str=" & For %%G in (!Str_!)Do Set "_Str=!_Str! %%~G"
- Set "C_Out=%~1"
- Set "_Str=!_Str:%1 =!" & For /F "Delims=" %%G in ("!_Str!")Do Set "_Str=%%~G"
- For %%G in (!_Str!) Do Set ".Str=%%G"
- PUSHD "%TEMP%\colorize" || (Echo/"%TEMP%\colorize" Could not be found & %CLOSE%)
- If /I "!.Str!" == "Exit" (Set "C_Out=04") Else If /I "!.Str!" == "Next" (Set "C_Out=02") Else If /I "!.Str!" == "Continue" (Set "C_Out=02") Else If /I "!.Str!" == "Back" (Set "C_Out=05") Else If /I "!.Str!" == "Return" (Set "C_Out=05")
- <nul set /p ".=%DEL%" > " !_Str!"
- findstr /v /a:!C_Out! /R "^$" " !_Str!" nul
- del " !_Str!" > nul 2>&1
- POPD
- Echo/
- Exit /B 0
- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Key variable and Macro definition
- :main
- ::::::::::::::::::::: [ For readablities sake - %Menu% macro is built from the following ]:
- rem ::: Order of definition must be preserved.
- rem [* prepare default findstr color for non windows 10 users *]
- IF NOT EXIST "%TEMP%\colorize" md "%TEMP%\colorize"
- For /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (set "DEL=%%a")
- rem [* default color args for Picked / Not Picked options. Overriden to Echo with Ascii Escape codes if Windows 10 *]
- rem [* Color /? for the full combination of colors - BG + FG colors must differ. [BG:a-f|0-9][FG:a-f|0-9] *]
- Set "_nP=Call :C_Out 08"
- Set "_P=Call :C_Out 06"
- Set "_Win10="
- Set "_End="
- rem [* Test if Windows 10 If true Overide defaults _nP and _P with Echo and VT codes for faster execution with windows 10 *]
- wmic OS get OSArchitecture,caption | FIND "10" >nul && ((For /F %%a in ('echo prompt $E ^| cmd')do Set "\E=%%a")&Set "_Win10=_")
- If Defined _Win10 (Set "_nP=Echo/%\E%[90m"& Set "_P=Echo/%\E%[33m")
- rem [* Menu supports 36 choices using _O array index with substring modification on _Cho var to index choice selection of Array Elements *]
- Set "_Cho=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- Set "DisplayArray=(Echo/!#Sel!| Findstr /LIC:"%%~G" > Nul 2> Nul && (%_P% {!_Cho:~%%z,1!} + %%~G) ) || (%_NP% [!_Cho:~%%z,1!] - %%~G)"
- Set "#Sel=_Nil"
- Set "ClearArray=(For /F "Tokens=1,2 Delims==" %%i in (' Set Opt[ 2^> Nul ')Do "Set %%i=")"
- Set "ResetVars=(Set "#L=F" & Set "OptL=" & Set "_O=0")"
- Set "CLOSE=Endlocal & Set "_End=Y" & Exit /B 0"
- Set "BuildArray=((If !_O! GTR 35 (Call :C_Out 04 "Maximum options [!_O!] Exceeded" & (Timeout /T 5 /NOBREAK) & %CLOSE%))&Set "OptL=!OptL!!_Cho:~%%z,1!"&Set "Opt[!_Cho:~%%z,1!]=%%~G")"
- Set "MakeChoice=(For /F "Delims=" %%C in ('Choice /N /C:!OptL!')Do Call :!Opt[%%C]! "Param" 2> Nul || ((Echo/"!#Sel!"| Findstr /LIC:"!Opt[%%C]!" > Nul 2> Nul && (For /F "Delims=" %%r in ("!Opt[%%C]!")Do If Not "!#Sel!" == "" (Set "#Sel=!#Sel:"%%r"=!")Else (Set "#Sel=_Nil"))) || (Set "#Sel=!#Sel! "!Opt[%%C]!"")))"
- Set "Return=For /L %%s in (0 1 4)Do (If not "!#Sel!" == "" (If "!#Sel:~0,1!" == " " (If "!#L!" == "F" (Set "#Sel=!#Sel:~1!"))Else (Set "#L=T"))Else (Set "#Sel=_Nil"))&if not "!#Sel!" == "_Nil" if not "!#Sel!" == "" (Set "#Sel=!#Sel:_Nil=!")"
- Set "Menu=(If defined _End Goto :End) &For %%n in (1 2)Do if %%n==2 (%ClearArray% & %ResetVars% &(For %%G in (!Options!)Do For %%z in (!_O!)Do %BuildArray% & %DisplayArray% &Set /A "_O+=1")& %MakeChoice% & %Return% )Else Set Options="
- For %%M in ( ClearArray ResetVars BuildArray DisplayArray MakeChoice Return )Do Set "%%M="
- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- Setlocal EnableExtensions EnableDelayedExpansion & REM [* required to be activated AFTER definition of Macro's. *]
- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Commence script main body | Demonstration of usages
- :Loop
- rem [* doublequoting of options recommended - Required for strings containing standard delimiters. *]
- If not defined _End cls
- rem [* Param Substring modification examples passing parameters to labels called when selected - Optional feature. *]
- Set /A "PRM=!Random! %%100 + 100" & Rem [* Example param only *]
- %Menu:Param=PRM% "Exit" "Next" "Option 1" "Option 2" "Option 3" "Option 4"
- Echo/"!#Sel!" | Findstr.exe /LIC:"Exit" > Nul 2> Nul && (Goto :End)
- Goto :Loop
- :Next
- rem [* Selection of a single option occurs by using the macro without a loop or resetting the #Sel variable
- rem - between %menu% use and next iteration of a loop *]
- rem [* Process #Sel items using the !#Sel! variable - then ** SET "#Sel-_Nil" prior to next usage of Menu macro** *]
- Set "Menu1Opts=!#Sel!"
- Set "#Sel="
- Cls
- Call :C_Out 03 "[Demo - Parameter usage. %%%%1 ~ '%1' == '!%~1!' ] Selected =" !Menu1opts!
- %Menu% "Exit" " + Continue" ".. Back"
- Echo/!#Sel! | Findstr.exe /LIC:".. Back" > Nul 2> Nul && (Set "#Sel=!Menu1opts!"& Exit /B 0)
- Echo/!#Sel! | Findstr.exe /LIC:"Exit" > Nul 2> Nul && (%CLOSE%)
- Set "#Sel="
- Echo/!Menu1opts! | Findstr.exe /LIC:"_Nil" > Nul 2> Nul && (Call :C_Out 04 "Selection Required."&(Pause & Exit /B 0)) || Call :C_Out 02 "Confirmed=" !Menu1opts!
- Call :C_Out 02 "Example complete."
- Pause
- rem [* to exit at end of script or by user selection *]
- %CLOSE%
- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: End of example
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement