创建批处理文件来识别活动的 Internet 连接
我正在尝试编写一个批处理文件,如果 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
Im trying to write a batch file which will allow a user to select their active Internet connection if there is more than one from a list generated by the netsh command and then change the DNS settings.
However I can't figure out how to use the choice command when the number of options are known until the script is executed. Without the use of arrays, I have tried to create a string variable 'choices' to hold the string representing the numerical choices and pass it to the choices command, but I cant get it to work. I can't help feeling that there must be a much easier way to do this but my research fails to show me so. Any help would be gratefully received.
@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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题不在于选择命令,选择字符串的构建失败。
有时一个简单的
echo on
会有所帮助。这会失败,因为
set choice=%choices%
仅在循环开始之前展开一次,因此您得到set choice=1%%G
。相反,您可以使用延迟扩展
或双/调用扩展
延迟扩展(部分)用
set /?
解释The problem is not at the choice command, the building of the choice-string fails.
Sometimes a simple
echo on
would help.This fails, because the
set choices=%choices%
is expanded only once before the loop starts, so you gotset choices=1%%G
.Instead you could use delayed expansion
or double/call expansion
The delayed expansion is (partially) explained with
set /?