I'm currently having a hard time figuring out how to get the active interface names as a variable output which can be later on used in the code. I've been reading here a bit, how to use the cmd output as a variable, but I need the specific names which are active.
My current code:
@echo off
netsh interface show interface
FOR /F "tokens=* USEBACKQ" %%F IN (`netsh interface show interface`) DO (
SET var=%%F
)
ECHO %var%
Pause
Which displays this image: We can see that due to
netsh interface show interface
, two connected interfaces and two non connected are shown. However, how do i get f.ex. Ethernet 2 and WiFi as a variable only like %%V ?
Because the output of WMIC has the 'ugly'
<CR><CR><LF>
line endings and the value ofNetConnectionID
will often have unwanted trailing spaces, I'd suggest something more like this to retrieve only connected network adapters as variables:to get the names of all interfaces that are connected:
Note: this is language dependent.
For a language independent solution use
wmic
(which has it's own traps and oddities):The inner
for
is to handle the uglywmic
line endings