DosTips.com

A Forum all about DOS Batch
It is currently 25 Jun 2017 04:30

All times are UTC-06:00




Post new topic  Reply to topic  [ 6 posts ] 
Author Message
PostPosted: 31 May 2014 21:41 
Offline
Expert

Joined: 06 Dec 2011 22:15
Posts: 1375
Location: México City, México
The creation of a multi-line menu that allows to select options via arrow keys has been an old request in Batch files. This type of selection menu may be developed with the aid of auxiliary programs, like GetKey to get the selection keys and a combination of CursorPos/ColorShow to deselect the current option and highlight the new one; however, such a program is somewhat complex.

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:

  1. Clear previous DOSKEY history.
  2. Execute several SET /P commands that read the menu options, so the DOSKEY history is filled with them.
  3. Send a F7 key to the keyboard.
  4. 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:

Image

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


Top
   
PostPosted: 01 Jun 2014 00:51 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 6033
You're a clever fellow Antonio. :)


Top
   
PostPosted: 01 Jun 2014 02:18 
Offline

Joined: 20 Aug 2010 13:57
Posts: 430
Location: Chile
Aacini: Very good idea.

Edit: for clear the buffer of command history set to 0 and then to the number of elements of the menu:

Code: Select all
rem Clear the command history
doskey /LISTSIZE=0
doskey /LISTSIZE=%numOpts%


or simple set the size to the number of elements of the menu:
Code: Select all
doskey /LISTSIZE=%numOpts%


also, i add the home key for the menu appears selected with the first option:
after F7:
Code: Select all
 wshShell.SendKeys("{HOME}");


doskey handle two buffers, the buffer of the doskey macros, and the buffer of the commands history. Apparently doskey /reinstall not works, it reset any buffer. Also /history show anything even when you see commands with F7.


Top
   
PostPosted: 15 Feb 2017 16:10 
Offline

Joined: 01 Oct 2012 13:32
Posts: 176
Location: Ireland
Hello All! :)

Here’s an alternative approach I cobbled together:

Code: Select all
@echo off & setlocal enableextensions disabledelayedexpansion
(call;)
title Menu Demo
mode con lines=15 cols=20
(set lf=^
%= DO NOT DELETE =%
)
set ^"nl=^^^%lf%%lf%^%lf%%lf%^"
set ^"\n=^^^%lf%%lf%^%lf%%lf%^^"

cls
echo(Choose an Option:%nl%%\n%
  1. Calculator%nl%%\n%
  2. Disk Cleanup%nl%%\n%
  3. Notepad%nl%%\n%
  4. Paint%nl%%\n%
  5. WordPad%nl%%\n%
  0. Quit

:readKey
set "opt=" & for /f skip^=1^ delims^=^ eol^= %%A in ('
replace ? . /u /w
') do if not defined opt set "opt=%%A"

set opt | findstr /ix "opt=[012345]" >nul || goto readKey

if %opt% equ 0 goto end

for /f "tokens=1,2 delims=:" %%A in (
^"1:calc%nl%2:cleanmgr%nl%3:notepad%nl%4:mspaint%nl%5:wordpad^"
) do if %opt% equ %%A (
start "" "%%B.exe"
goto end
)

:end
endlocal & goto :EOF


Run the program from Windows/File Explorer. All key presses except 0-5 will be ignored.

Enjoy!

- SB

PS: Thanks to Carlos for the Read Key with REPLACE technique. 8)


Top
   
PostPosted: 16 Feb 2017 02:13 
Offline

Joined: 04 Feb 2016 16:36
Posts: 1
Is there anyway to disable the left and right cursor?
Because if i press does, it hide the menu and show line by line.
Thanks


Top
   
PostPosted: 18 Feb 2017 09:21 
Offline
Expert

Joined: 06 Dec 2011 22:15
Posts: 1375
Location: México City, México
Sponge Belly wrote:
Hello All! :)

Here’s an alternative approach I cobbled together:

Code: Select all
snip


Run the program from Windows/File Explorer. All key presses except 0-5 will be ignored.

Enjoy!

- SB

PS: Thanks to Carlos for the Read Key with REPLACE technique. 8)


Mmmm... As far as I know, the REPLACE technique can not read arrow keys either. Your example code could be done in a simpler way via CHOICE command...

Antonio


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 6 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: mogodan2008 and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited