.bat文件中的可滚动列表 [英] Scrollable Lists in .bat Files

查看:158
本文介绍了.bat文件中的可滚动列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用批处理文件中的箭头键创建可滚动列表?选择一个之后,它可以执行以下操作:(goto result1)等吗?如果不可能,我只需要坚持自己输入答案即可.

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.

推荐答案

下面的批处理文件使用了一个有趣的技巧,其中包括用列表中的元素填充DOSKEY历史记录.之后,将F7键发送到键盘,因此在执行SET/P命令时,先前的元素会显示在由DOSKEY管理的可滚动列表(菜单选择)中.

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:

先前的程序是Batch-JScript混合脚本;您可能会看到这篇文章以获取有关混合的解释脚本和进一步了解这个程序.

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.

这篇关于.bat文件中的可滚动列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆