I devised a simpler method to achieve such selection menu using DOSKEY program. This DOS command store the lines executed in the command-line (like inputs to SET /P command) and store they in a history that may be displayed in the form of a selection menu when F7 key is pressed. This way, the method consist in:
- Clear previous DOSKEY history.
- Execute several SET /P commands that read the menu options, so the DOSKEY history is filled with them.
- Send a F7 key to the keyboard.
- Execute a SET /P "OPTION=Prompt: "; the input to this command will be completed via the selection menu of DOSKEY.
Code: Select all
@if (@CodeSection == @Batch) @then
@echo off
setlocal EnableDelayedExpansion
rem Multi-line menu with options selection via DOSKEY
rem Antonio Perez Ayala
rem Define the options
set numOpts=0
for %%a in (First Second Third Fourth Fifth) do (
set /A numOpts+=1
set "option[!numOpts!]=%%a Option"
)
set /A numOpts+=1
set "option[!numOpts!]=exit"
rem Clear previous doskey history
doskey /REINSTALL
rem Fill doskey history with menu options
cscript //nologo /E:JScript "%~F0" EnterOpts
for /L %%i in (1,1,%numOpts%) do set /P "var="
:nextOpt
cls
echo MULTI-LINE MENU WITH OPTIONS SELECTION
echo/
rem Send a F7 key to open the selection menu
cscript //nologo /E:JScript "%~F0"
set /P "var=Select the desired option: "
echo/
if "%var%" equ "exit" goto :EOF
echo Option selected: "%var%"
pause
goto nextOpt
@end
var wshShell = WScript.CreateObject("WScript.Shell"),
envVar = wshShell.Environment("Process"),
numOpts = parseInt(envVar("numOpts"));
if ( WScript.Arguments.Length ) {
// Enter menu options
for ( var i=1; i <= numOpts; i++ ) {
wshShell.SendKeys(envVar("option["+i+"]")+"{ENTER}");
}
} else {
// Enter a F7 to open the menu
wshShell.SendKeys("{F7}");
}
Output example of previous program:
There is a strange point about this program: the DOSKEY /REINSTALL command clear the history only when it is executed the first time. If previous program is executed a second time in the same cmd.exe session, the DOSKEY history is not cleared, so menu options are wrong. I read DOSKEY documentation in Microsoft and SS64 sites and didn't found any reference about this problem. I tested this program in Windows 8, so I don't know if this problem also appear in other versions...
Antonio