创建批处理文件来识别活动的 Internet 连接

发布于 2024-10-25 23:14:43 字数 1024 浏览 2 评论 0原文

我正在尝试编写一个批处理文件,如果 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

腻橙味 2024-11-01 23:14:43

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

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

这会失败,因为 set choice=%choices% 仅在循环开始之前展开一次,因此您得到 set choice=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 /? 解释

The problem is not at the choice command, the building of the choice-string fails.
Sometimes a simple echo on would help.

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

This fails, because the set choices=%choices% is expanded only once before the loop starts, so you got set choices=1%%G.

Instead you could use delayed expansion

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

or double/call expansion

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

The delayed expansion is (partially) explained with set /?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文