Search code examples
variablesinterfacebatch-processingnetshwifi

Suppress space before and after for /f netsh wlan show interface


OK, so the below script removes the spaces from before and after, and it works now. I am sure there are much better ways to write this.

    setlocal enableextensions enabledelayedexpansion
    for /f "tokens=1-2 delims=:" %a in ('netsh wlan show interface^|find "Name"') do (echo %b >> c:\temp\WLANINT.txt)
    for /f "delims=" %x in (c:\temp\WLANINT.txt) do SET WLANINT=%x
    :SpaceX
    echo "%WLANINT%"
    IF "%WLANINT:~0,1%"==" " (SET WLANINT=%WLANINT:~1,-1%)
    echo "%WLANINT%"
    IF "%WLANINT:~0,1%"==" " GOTO SpaceX
    echo "%WLANINT%"
    for /l %a in (1,1,100) do if "!WLANINT:~-1!"==" " set WLANINT=!WLANINT:~0,-1!
    echo."%WLANINT%"
    netsh wlan set profileorder name="%WIFI%" interface="%WLANINT%" priority=1
    echo "%WLANINT%"

Solution

  • Try next code snippet (save as 31194241.bat; running it might require administrative privileges):

    @ECHO OFF >NUL
    SETLOCAL enableextensions
    for /f "tokens=1,* delims=:" %%a in ('
        netsh wlan show interface^|find "Name"
        ') do for /f "tokens=*" %%x in ("%%b") do (
      echo netsh wlan set profileorder name="%WIFI%" interface="%%x" priority=1
    )
    

    Changes made to your script (read more):

    • in all %a, %b, %c doubled percent signs as %%a, %%b, %%c for using in a .bat script;
    • nested loops instead of creating an auxiliary file;
    • for /f "tokens=*" will left trim blak space(s);
    • productive netsh command is merely echoed for debugging purposes only (replace echo netsh with netsh no sooner than debugged).

    Instead of batch, one could use an one-liner from command line:

    for /f "tokens=1,* delims=:" %a in ('netsh wlan show interface^|find "Name"') do @for /f "tokens=*" %x in ("%b") do @echo netsh wlan set profileorder name="%WIFI%" interface="%x" priority=1