사용자가 netsh 명령에 의해 생성 된 목록에서 둘 이상인 경우 활성 인터넷 연결을 선택한 다음 DNS를 변경하도록하는 배치 파일을 작성하려고합니다. 설정.활성 인터넷 연결을 식별하는 배치 파일 만들기
그러나 스크립트가 실행될 때까지 옵션 수가 알려지면 선택 명령을 사용하는 방법을 알 수 없습니다. 배열을 사용하지 않고 숫자 선택을 나타내는 문자열을 보유하고 선택 명령에 전달하는 문자열 변수 '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