ipodlover3354
ipodlover3354

Reputation: 469

Scrollable Lists in .bat Files

Is it possible to make a scrollable list with the arrow keys in batch files? After selecting one, can it do something like: (goto result1) etc? If it isn't possible, I just have to stick with the user inputting answers themselves.

Upvotes: 1

Views: 1684

Answers (2)

Aacini
Aacini

Reputation: 67236

The Batch file below use an interesting trick that consist in fill the DOSKEY history with the elements of the list. After that, a F7 key is send to the keyboard, so when a SET /P command is executed previous elements are displayed in a scrollable list (menu selection) managed by DOSKEY.

@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:

Example of previous program

Previous program is a Batch-JScript hybrid script; you may see this post for an explanation of hybrid scripts, and this one for a further description of this program.

Upvotes: 4

Stephan
Stephan

Reputation: 56208

It's quite easy if you need filenames. For other lists, it's quite difficult in pure batch.

echo select file with [TAB]
set /p "file=Select file name: "
echo you selected %file%

will toggle through all files in the directory if you press TAB.

You can also give the first two or three characters and then toggle through all matching files with TAB

It's even possible to give *.txt and then toggle through all .txt files in the current directory.

Upvotes: 0

Related Questions