Reputation: 63
I can't seem to find this question anywhere else, but, how do I retrieve the name of the currently connected wireless network. If it's necessary, my aim of the program is to disconnect from the internet for 10 seconds, then to reconnect. Here's my code:
@echo off
ipconfig /release
timeout /t 10 /nobreak
netsh wlan connect %Network%
I just need help getting the exact name of the wireless network that I am currently connected to, to fit in %Network%
. I'd like any help.
I'm also a beginner in batch so yeah.
Upvotes: 3
Views: 10836
Reputation: 41932
To get network information use netsh wlan show interface
. You can further filter the interface out with name=
. In the output SSID will be in the SSID
field and probably also Profile
field which can be extracted with findstr
. Data can then be read into a variable with for /f
for /f "delims=: tokens=2" %%n in ('netsh wlan show interface name="Wi-Fi" ^| findstr "SSID"') do set "Network=%%n"
set "Network=%Network:~1%"
The last line is for removing the first space character using variable substring
Of course this assumes you have only one WLAN adapter. If you have more than one then things are more complicated and you have several choices
Another option is to use
netsh wlan show networks interface="Wi-Fi" mode=ssid
Upvotes: 6
Reputation: 5874
Taken from this answer and modified:
As you can find in linked answer, the general command to get the information you want is wmic nic where "NetConnectionStatus=2" get NetConnectionID
. With | more +1
you can pass the header that would be displayed in the wmic-command alone.
To get this into a variable you want to parse the output of that command with for /f
:
for /f %%n in ('wmic nic where "NetConnectionStatus=2" get NetConnectionID ^| more +1') do (
set "Network=%%n"
goto outer
)
:outer
I use the goto to exit the loop on the first iteration and only use it to parse the first line. As the output of the command contains some empty lines this would then leave you with an empty variable if the label would not be there.
Upvotes: -1