0

I'm trying to rename the NIC names on my PC. It has A LOT of NICs (they are virtual). I can get their names like so

for /f "tokens=2 delems==" %%A in ('wmic nic where "Description like 'foo'" get netconnection id /value') do (
 echo %%A
)

I'd like to use netsh inside that loop to rename them. Unfortunately, the target names aren't simple incrementing numbers. So I did

set names[0]=name1
set names[1]=name two
set names[2]=name test
set local ENABLEDELAYEDEXPANSION
set /a idx=0
for /f "tokens=2 delems==" %%A in ('wmic nic where "Description like 'foo'" get netconnectionid /value') do (
 echo names[!idx!]
 set /a idx=!idx!+1
)

This outputs

names[0]
names[1]
names[2]

But if I try

set names[0]=name1
set names[1]=name two
set names[2]=name test
set local ENABLEDELAYEDEXPANSION
set /a idx=0
for /f "tokens=2 delems==" %%A in ('wmic nic where "Description like 'foo'" get netconnectionid /value') do (
 echo %names[!idx!]%
 set /a idx=!idx!+1
)

I just get garbage output. Is there some way I can evaluate the generated string in the previous example as a variable? Then I could pass that to netsh to do the renaming.

Thundercleez
  • 327
  • 1
  • 4
  • 18
  • See [this answer](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990) – Aacini Mar 29 '17 at 19:42
  • Yes, that's the reference I used to get as far as I have before posting this question. – Thundercleez Mar 29 '17 at 19:49
  • You are mixing up `Set /P` with `Set /A`. Second is the property is named `NetConnectionID` no space in between. –  Mar 29 '17 at 19:58

2 Answers2

2

In first place, your code have a couple errors. This line:

set local ENABLEDELAYEDEXPANSION

... should be written this way:

setlocal ENABLEDELAYEDEXPANSION

Otherwise the "setlocal" command becomes a "set" one, and don't works in the same way.

In this line:

for /f "tokens=2 delems==" %%A in (. . .

... the option must be written "delims==" (NOT "delems=="), but in this case the FOR command issue an error.

After these errors were fixed, you should use the method to access array elements fully described at this answer, so I copy the relevant parts here:

To get the value of an element when the index changes inside FOR/IF, enclose the element in double percent symbols and precede the command with call:

call echo %%names[!idx!]%%

Another way to achieve the previous process is to use an additional FOR command to change the delayed expansion of the index by an equivalent replaceable parameter, and then use the delayed expansion for the array element. This method runs faster than previous CALL:

for %%i in (!idx!) do echo !names[%%i]!
Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
1

I eventually figured out a way to do this, but it only works when trying to index into an array of numbers. This is what I used

@ECHO OFF
set names[1]=101
set names[2]=102
set names[3]=103

setlocal ENABLEDELAYEDEXPANSION

set /a idx=1
for /L %%A in (1,1,3) do (
 set /a foo = names[!idx!] #This is the key, for some reason, an intermediate variable is required.
 echo !foo!
 set /a idx=!idx!+1
)
Thundercleez
  • 327
  • 1
  • 4
  • 18