Search code examples
windowsbatch-filecmdtxtnetsh

Batch file finding strings for Current network SSID


I need to assign the current network/wifi SSID to an environment variable in a batch file (set "var=x) to then use it. My script saves a file with the SSID, but it's the entire lines, i need the variable to only be the SSID value. Here is my code:

@echo off
set "npcm=%appdata%\npcm.txt"
2>NUL netsh wlan show interfaces | findstr /i "SSID" > "%npcm%"
pause

The output file is:

    SSID                   : MyNetwork
    BSSID                  : xx:xx:xx:xx:xx:xx

I tried using the following lines of code but it doesn't work, also the %a was %%a but after some researches i found out i need 1 % only.

for /f "tokens=2 delims='" %a in ('type file.h ^|find "#define SW_VERSION_BYTE_"') do set "var=!var!%a"

Solution

  • The command line for usage in a batch file: set "SSID=" & for /F "tokens=1* delims=: " %%H in ('%SystemRoot%\System32\netsh.exe wlan show interfaces 2^>nul ^| %SystemRoot%\System32\findstr.exe /R "\<SSID\>"') do set "SSID=%%I" The same command line for usage directly in a command prompt window: set "SSID=" & for /F "tokens=1* delims=: " %H in ('%SystemRoot%\System32\netsh.exe wlan show interfaces 2^>nul ^| %SystemRoot%\System32\findstr.exe /R "\<SSID\>"') do @set "SSID=%I" SSID is undefined on no WLAN interface with an SSID available at all.

    – Mofi

    This was in the comments