创建一个批处理文件来确定活动的Internet连接 [英] Creating a batch file to identify the active Internet connection

查看:167
本文介绍了创建一个批处理文件来确定活动的Internet连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去编写一个批处理文件,这将允许用户选择自己的互联网连接,如果有一个以上的由netsh命令生成的列表,然后更改DNS设置。

不过,我无法弄清楚如何使用选择命令时,直到执行脚本被称为选项的数目。如果不使用数组,我曾尝试创建一个字符串变量选择来保存字符串重新presenting数值的选择,它传递到选择的命令,但我不能得到它的工作。我不禁觉得必须有这样做更简单的方法,但我的研究失败对我如此。任何帮助将受到欢迎。

 关闭@echo
SETLOCAL
设置激活= 0
设置选项= 1
设置ConnnectedNet =
FOR / F令牌= 2,3 *号%%Ĵ(netsh接口显示接口^ |找到连接')做套装/ A主动+ = 1
FOR / L %% G IN(2,1,%活性%),做(设置选项=%选择%%% G)
如果%活性%LSS页转到:单
如果%活性%GTR 1页转到:多
:单
FOR / F令牌= 2,3 *号%%Ĵ(netsh接口显示接口^ |寻找已连接)并设置ConnnectedNet = %%升
netsh接口的IPv4设置的dnsserver%ConnnectedNet%静态两种0.0.0.0
GOTO:EOF
:多
回声你有一个以上的活动接口。请选择您正在使用连接到Internet的接口
FOR / F令牌= 2,3 *号%%Ĵ(netsh接口显示接口^ |寻找已连接)也呼应%%升
CHOICE / C:选择%%/ N / T:1,10


解决方案

问题是不是在选择命令,选择串建设失败。结果
有时候,一个简单的 echo开会有所帮助。

 设置选项= 1
...
FOR / L %% G IN(2,1,%活性%),做(设置选项=%选择%%% G)

这失败,因为设置选项=%选择%之前只有一次的扩大循环开始,所以你得到了设置选项= 1 %%摹

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

  SETLOCAL EnableDelayedExpansion
FOR / L %% G IN(2,1,%活性%),做(设置选项=!选择!%% G)

或双/调用扩展

  FOR / L %% G IN(2,1,%活性%)做(通话设置选择= %%选择%%%% G)

延迟扩展(部分)与解释设置/?

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

解决方案

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 /?

这篇关于创建一个批处理文件来确定活动的Internet连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆