Home > Software design >  How do I handled a pound sign in a batch script naming network adapters?
How do I handled a pound sign in a batch script naming network adapters?

Time:02-03

This script is grabbing the device names, then renaming the adapter to make connections easier on the user. I've tried double quotes, spaces, parenthesis, and nothing works. This is Windows 10 Pro 1607. Running as admin.

How am I supposed to handle the # and space between "network connection" and "2" on the 6th line?

REM @echo off
SetLOCAL

REM REQUIRED FOR INTERMEDIATE VARIABLES
setlocal EnableDelayedExpansion

REM DEVICE NAMES
set nic_TEST1="Intel(R) Ethernet Connection (7) I219-LM"
set nic_TEST2="Intel(R) I210 Gigabit Network Connection"
set nic_TEST3="Intel(R) I210 Gigabit Network Connection #2"

REM FIND DEVICE INDEX (TEST2)
for /f "tokens=*" %%a in ('route print^|find %nic_TEST2%') do (
    set net_TEST2_line=%%a
    set nic_TEST2_index=!net_TEST2_line:~0,2!
    )

REM FIND INTERFACE NAME (TEST2)
for /f "usebackq tokens=1,5,* skip=3" %%a in (`netsh interface ipv4 show interface`) do (
    if "%%a"=="%nic_TEST2_index%" set nic_TEST2_interface=%%b %%c
    )

REM FIND DEVICE INDEX (TEST1)
for /f "tokens=*" %%a in ('route print^|find %nic_TEST1%') do (
    set net_TEST1_line=%%a
    set nic_TEST1_index=!net_TEST1_line:~0,2!
    )

REM FIND INTERFACE NAME (TEST1)
for /f "usebackq tokens=1,5,* skip=3" %%a in (`netsh interface ipv4 show interface`) do (
    if "%%a"=="%nic_TEST1_index%" set nic_TEST1_interface=%%b %%c
    )

REM FIND DEVICE INDEX (TEST3)
for /f "tokens=*" %%a in ('route print^|find %nic_TEST3%') do (
    set net_TEST3_line=%%a
    set nic_TEST3_index=!net_TEST3_line:~0,2!
    )

REM FIND INTERFACE NAME (TEST3)
for /f "usebackq tokens=1,5,* skip=3" %%a in (`netsh interface ipv4 show interface`) do (
    if "%%a"=="%nic_TEST3_index%" set nic_TEST3_interface=%%b %%c
    )

REM STRIP TRAILING SPACE
for /f "tokens=* delims= " %%a in ('echo %nic_TEST2_interface% ') do set nic_TEST2_interface=%%a
set nic_TEST2_interface=%nic_TEST2_interface:~0,-1%
for /f "tokens=* delims= " %%a in ('echo %nic_TEST1_interface% ') do set nic_TEST1_interface=%%a
set nic_TEST1_interface=%nic_TEST1_interface:~0,-1%
for /f "tokens=* delims= " %%a in ('echo %nic_TEST3_interface% ') do set nic_TEST3_interface=%%a
set nic_TEST3_interface=%nic_TEST3_interface:~0,-1%

REM DELETE SCRIPT IF INTERFACES ARE SET
if "%nic_TEST2_interface%"=="TEST2" (
if "%nic_TEST1_interface%"=="TEST1" (
        if "%nic_TEST3_interface%"=="TEST3" (
        del /q /f %0 & exit
        )
        ) 
        )

REM EXIT IF DEVICE IS NOT PRESENT
if "%nic_TEST2_index%"=="" exit
if "%nic_TEST1_index%"=="" exit
if "%nic_TEST3_index%"=="" exit

REM RENAME INTERFACES
netsh interface set interface name="%nic_TEST2_interface%" newname="TEST2"
netsh interface set interface name="%nic_TEST1_interface%" newname="TEST1"
netsh interface set interface name="%nic_TEST3_interface%" newname="TEST3"

REM RERUN SO SCRIPT CAN SELF-DELETE
%0

I've tried quotations, spacing, parenthesis and ^ in various forms. I know the rest of my code works because the other two adapters are named correctly.

CodePudding user response:

There's an easier way of doing this. You could accomplish this in 3 lines instead of 80.

Netsh interface set interface name="TEST1" 
new name="desired name"
Netsh interface set interface name="TEST2" 
new name="desired name"
Netsh interface set interface name="TEST3" 
new name="desired name"

CodePudding user response:

It has nothing to do with #, but you're not providing us with a sample listing of the target data from the route and netsh commands.

Use set "var=value" for setting string values - this avoids problems caused by trailing spaces. Don't assign " or a terminal backslash or Space. Build pathnames from the elements - counterintuitively, it is likely to make the process easier. If the syntax set var="value" is used, then the quotes become part of the value assigned.

I'll assume you follow this advice as dealing with quotes in awkward places is a nightmare.

find will detect both nic_B and nic_c since nic_b is a substring of nic_c

Try using |findstr /e /L /c:"%nic_c%" which finds the /L literal string /C:"this string" at the /e end of a line.

I'll presume that this string occurs at the end of the line since my route print produces wildly different results (which is why you need to show us your results).

  • Related