gpt4 book ai didi

windows - 创建批处理文件以识别事件的 Internet 连接

转载 作者:可可西里 更新时间:2023-11-01 10:28:53 26 4
gpt4 key购买 nike

我正在尝试编写一个批处理文件,如果 netsh 命令生成的列表中有多个,则允许用户选择他们的事件 Internet 连接,然后更改 DNS 设置。

但是,在执行脚本之前,当已知选项数量时,我不知道如何使用 choice 命令。在不使用数组的情况下,我试图创建一个字符串变量“choices”来保存表示数字选择的字符串并将其传递给 choices 命令,但我无法让它工作。我不禁觉得一定有一种更简单的方法可以做到这一点,但我的研究并没有证明这一点。我们将不胜感激。

@echo off
setlocal
Set active=0
Set choices=1
set ConnnectedNet=
FOR /F "tokens=2,3* " %%j in ('netsh interface show interface ^| find "Connected"') do Set /A active+=1
FOR /L %%G IN (2,1,%active%) do (set choices=%choices%%%G)
if %active% lss 2 goto :single
if %active% gtr 1 goto :multiple
:single
FOR /F "tokens=2,3* " %%j in ('netsh interface show interface ^| find "Connected"') do set ConnnectedNet=%%l
netsh interface IPv4 set dnsserver "%ConnnectedNet%" static 0.0.0.0 both
goto :eof
:multiple
echo You have more than one active interface. Please select the interface which you are using to connect to the Internet
FOR /F "tokens=2,3* " %%j in ('netsh interface show interface ^| find "Connected"') do echo %%l
CHOICE /C:%choices% /N /T:1,10

最佳答案

问题不在选择命令,选择字符串的构建失败。
有时一个简单的 echo on 会有所帮助。

set choices=1
...
FOR /L %%G IN (2,1,%active%) do (set choices=%choices%%%G)

这失败了,因为 set choices=%choices% 只在循环开始之前展开一次,所以你得到了 set choices=1%%G

相反,您可以使用延迟扩展

setlocal EnableDelayedExpansion  
FOR /L %%G IN (2,1,%active%) do (set choices=!choices!%%G)

或双倍/调用扩展

FOR /L %%G IN (2,1,%active%) do (call set choices=%%choices%%%%G)

延迟扩展(部分)用 set/? 解释

关于windows - 创建批处理文件以识别事件的 Internet 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5435884/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com