我已经知道如何使用
alphabet sequence
添加 ExitCodeAscii
菜单,改编自:Incrementing alphabet...
,
但我认为这种方法使输出慢。
有谁知道一个更快的方法和简单的代码?
我试过这个:
:_Menu
@echo off
set "AZ1=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "AZ2=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
set "menu= one two three four five"
for %%A in (%AZ2%) do for %%M in (%menu%) do echo [%%A] %%M
set "_Choice=" &rem ????
rem choice /c !_Choice! /m "Choose...:"
rem if %Errorlevel%==""
pause
goto :_Menu
输出:
[A] one
[A] two
[A] three
[A] four
[A] five
[B] one
[B] two
[B] three
如何进行输出:
[A] one
[B] two
[C] three
[D] four
[E] five
每次运行外部循环时都会执行内部
for
循环。你不想这样。@echo off
setlocal enabledelayedexpansion
set "AZ1= ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "menu= one two three four five"
set count=0
set "list= "
for %%M in (%menu%) do (
set /a count+=1
call set "list=!list!%%AZ1:~!count!,1%%"
call echo [%%AZ1:~!count!,1%%] %%M
set "val[!count!]=%%M"
)
choice /c %list% /m "Choose:"
set "choice=!list:~%errorlevel%,1!"
echo/
echo you pressed [!list:~%errorlevel%,1!:%errorlevel%] !val[%errorlevel%]!
编辑包括保存选项以供以后使用的附加请求。
@ECHO OFF
SETLOCAL
set "AZ1=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "AZ2=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
set "menu= one two three four five"
for %%M in (%menu%) do SET "assign=y"&for %%A in (%AZ2%) do IF DEFINED assign IF NOT DEFINED option[%%A] SET option[%%A]=%%M&SET "assign="&ECHO [%%A] %%M
SET option
GOTO :EOF
通常的做法是在初始
setlocal
之后直接使用 @echo off
命令。这会在批处理结束时丢弃对环境所做的任何更改,因此由一个批处理文件建立的变量不会影响可能在同一会话中运行的任何其他批处理。
如果您从“快捷方式”运行,错误消息将不可见(它们会出现,但批处理会继续并将其清除)。您可以在语句后放置
pause
并返回错误,但最好打开“命令提示符”并从那里运行批处理,以便窗口保持打开状态并显示任何(错误)消息.
快速启动菜单
固件启动、OSLoader 启动、重启、Bios 设置等
完成基于@squasman的脚本编辑改编动态菜单系统。
请检查是否有不正确的地方,我很乐意要求纠正
@echo off
goto:GetListMenu
:QuickBoot
cls
CALL :MENU %All_OS_Des% %_OtherBoot%
CALL :CHOSE
if /i %num% GTR !countwin! (call :OtherBoot) else (call :BootOS)
Goto:QuickBoot
:: #### END OF QuickBoot ####
rem Author: @Mr.Key7 : https://stackoverflow.com/users/23414967/mr-key7
rem Adapted from @squashman: https://stackoverflow.com/questions/67539331/what-am-i-doing-wrong-using-the-choice-command-with-batch/67542833#67542833
================================================================================================
:GetListMenu
setlocal EnableDelayedExpansion
set "FW_List="& set "WI_List="
set "countfw=0"
set "fwos=bcdedit /enum firmware ^| findstr "des" ^| findstr /v "PCI CD Setup Splash FDD Windows""
for /f "Tokens=2-4" %%B in ('%fwos%') do set /a "countfw+=1"& set "FW_List=!FW_List!"%%B %%C %%D" "
set "countwin=!countfw!"
set "wios=bcdedit /enum osloader ^| findstr "des" ^| find /v "Recovery""
for /f "Tokens=2-4" %%B in ('%wios%') do set /a "countwin+=1"& set "WI_List=!WI_List!"%%B %%C %%D" "
set "_OtherBoot="ReOpen" "ReBoot" "ShutDown" "SafeMode" "Bios Setup" "A. StarUp" "Exit""
rem echo [FWOS:!countfw!] [FWOS^+WINOS:!countwin!]
set "All_OS_Des=!FW_List!!WI_List!"
Goto:QuickBoot
:BootOS
for /f "tokens=1-2" %%D in ('echo %Option%') do set "F_DE=%%D %%E"
if /i %num% LEQ %countfw% (set "store=firmware") else (set "store=osloader")
for /F "usebackq tokens=1-3" %%a in (`bcdedit /enum %store% ^| findstr "den des"`) DO (
if /I "%%a"=="identifier" set "ID=%%b"
if /I "%%b %%c"=="!F_DE!" set "F_ID=!ID!"
)
if /I "!F_ID!"=="" (echo.& echo FW/OS Not Found& timeout 2 >nul & exit /b)
if /I "!F_DE!"=="USB HDD:" call :USBCheck
rem echo.& echo Boot to %Option%^?
rem echo !F_ID!
rem set Opt=
rem set /p Opt=". X=Cancel | Enter=Boot :"
rem if /I "%Opt%"=="x" exit /b
if /i %num% LEQ %countfw% ( bcdedit /set {fwbootmgr} bootsequence !F_ID!
) else ( bcdedit /bootsequence !F_ID! )
shutdown /r /t 00 &exit
:OtherBoot
if /i "%Option%"=="Bios Setup" shutdown /r /fw /t 00 &exit
if /i "%Option%"=="ReBoot" shutdown /r /t 00 &exit
if /i "%Option%"=="A. StartUp" shutdown /r /o /f /t 00 &exit
if /i "%Option%"=="ShutDown" shutdown /s /t 00 &exit
if /i "%Option%"=="ReOpen" start %~nx0 &exit
if /i "%Option%"=="SafeMode" call :SafeMode
if /i "%Option%"=="Exit" &exit
exit /b
:SafeMode
wmic computersystem get BootupState | Find "Normal" >nul || call :ExitSafeB
bcdedit /set {current} safeboot minimal && call :BootS
:ExitSafeB
bcdedit /enum {current} | Find "safeboot" >nul && bcdedit /deletevalue {current} safeboot
:BootS
bcdedit /bootsequence {current}& shutdown /r /t 00& exit
:USBCheck
wmic diskdrive where InterfaceType='USB' get InterfaceType 2> nul | Find "USB" >nul || (
echo insert an USB Drive first &timeout 3 >nul & goto :QuickBoot)
================================================================================================
REM Credit @squashman : https://stackoverflow.com/users/1417694/squashman
:MENU
set /a "OptionNo=0"
set "choices="
FOR %%I IN (%*) do (
set /a OptionNo+=1
call :ResolveChar !OptionNo!
set "Option!retval!=%%~I"
set "choices=!choices!!retval!"
echo [!retval!] %%~I
)
GOTO :EOF
:ResolveChar
SETLOCAL
set "keys=_ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
set "_startchar=%1"
CALL SET "char=%%keys:~%_startchar%,1%%"
ENDLOCAL & SET "retval=%char%"
goto :eof
:CHOSE
choice /c !choices! /m "Choose: " &rem /n
set "num=%errorlevel%"
call :ResolveChar %errorlevel%
set "Option=!Option%retval%!"
echo You Selected: [%retval%:%num%] - %Option%
GOTO :EOF
我的电脑上的输出:
[A] rEFInd Boot Manager
[B] MX23 (LinuX)
[C] Boot Menu
[D] USB HDD:
[E] ATA HDD: ADATA
[F] ThrottleStop UEFI
[G] \WIMBoot\DiskGenius-V5.5.0.1488
[H] \WIMBoot\EaseUS Partition Master
[I] \WIMBoot\EasyUEFI-v5.2-WinPE
[J] \WIMBoot\MiniTool.Partition.12.6.x64
[K] \WIMBoot\WinPE11x64-www.aioboot.com
[L] Windows 7 Ultimate
[M] \WimBoot\AOMEI_BackupperTP_v7.3
[N] DiskGenius - Boot
[O] Windows 10 Pro
[P] Windows 11 Pro
[Q] ReOpen
[R] ReBoot
[S] ShutDown
[T] SafeMode
[U] Bios Setup
[V] A. StarUp
[W] Exit
Choose: [A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W]?