微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

创build一个batch file来识别活动的Internet连接

我试图写一个batch file,这将允许用户select他们的活动Internet连接,如果从netsh命令生成的列表中有多个,然后更改DNS设置。

然而,我不知道如何使用select命令时,已知的选项数量,直到脚本执行。 没有使用数组,我试图创build一个stringvariables“select”来容纳代表数字select的string,并将其传递给select命令,但我不能让它工作。 我不禁感到必须有一个更简单的方法来做到这一点,但是我的研究却没有向我certificate这一点。 任何帮助将受到感谢。

@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

在Windows命令行中运行多个命令

用批处理从文件中读取带有空格的行

在Windows Batch中嵌套循环中的“继续”等效命令

复制未知目录中来自txt的最新文件

将特定列从一个文本文件复制到另一个文本文件

我可以更改单个窗口的PATHvariables吗?

奇怪的标准输出redirect行为

使用“IF”命令创buildbatch file

批处理脚本使其他batch file访问setlocalvariables

文件夹的上下文菜单中运行batch file

问题不在于选择命令,选择字符串的构建失败。

有时一个简单的echo on会有所帮助。

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

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

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

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

或双倍/呼叫扩展

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐