1

trying to get a batch to output network adapter names for another batch file. so far this works...

@echo off
set ignore=true
for /F "delims=" %%a in ('netsh interface show interface')do call :Sub %%a
goto :eof

:sub
if not "%adapter1%" EQU "" goto :2
set Line=%*
if "%Line:~0,10%" EQU "----------" (set ignore=false & goto :eof)
if %ignore% EQU true goto :eof
for /F "tokens=4*" %%b in ('echo %*') do set Adapter1=%%b
echo %Adapter1%
goto :eof
)

:2
for /F "tokens=4*" %%c in ('echo %*') do set Adapter2=%%c
echo %adapter2%
pause

But is there a way to loop the second part so the output would continue with Adapter#="Adapter Name" until there are no adapters left.

I've tried to use..

set /a c=1
    :sub
for /F "tokens=4*" %%c in ('echo %*') do (
set /a c=c+1
Set Adapter%c%=%%b
echo %adapter2%
)

couple of issues here trying to call a variable made of Variables ie.%adapter%c%%

and the other being i have no idea how to loop this back to the next line.

I realise i could keep expanding this but it would be horrendous.

@echo off
set ignore=true
for /F "delims=" %%a in ('netsh interface show interface')do call :Sub %%a
goto :eof

:sub
if not "%adapter2%" EQU "" goto :3
if not "%adapter1%" EQU "" goto :2
set Line=%*
if "%Line:~0,10%" EQU "----------" (set ignore=false & goto :eof)
if %ignore% EQU true goto :eof
for /F "tokens=4*" %%b in ('echo %*') do set Adapter1=%%b
echo %Adapter1%
goto :eof
)

:2
for /F "tokens=4*" %%c in ('echo %*') do set Adapter2=%%c
echo %adapter2%
pause

:3
for /F "tokens=4*" %%c in ('echo %*') do set Adapter3=%%c
echo %adapter3%
pause

Frustrated >.< , can't get my head around how its supposed to work. Any help appreciated. Thanks

2
  • 2
    for /F "skip=3 tokens=4*" %a in ('netsh interface show interface') do @echo %a (try it from an open cmd prompt)
    – JosefZ
    Commented Mar 15, 2019 at 20:41
  • Thank you JosefZ Commented Mar 18, 2019 at 10:11

1 Answer 1

2
  • It's much easier to skip the first 3 lines of the output
  • As the adapter name could contain spaces your approach with tokens=4 can't work,
    use an asterisk to catch the remainder of the parsed line in the next for var.

:: Q:\Test\2019\03\15\SO_55189424.cmd
@Echo off&SetLocal EnableDelayedExpansion

Set Cnt=0
for /f "skip=3 tokens=1-3*" %%A in ('
    netsh interface show interface
') Do (
    Set /A Cnt+=1
    Set "Adapter!Cnt!=%%D"
)
Set Adapter

Sample Output:

> Q:\Test\2019\03\15\SO_55189424.cmd
Adapter1=VirtualBox Host-Only Network
Adapter2=Ethernet
Adapter3=Ethernet 2
4
  • This is Awesome, Thank you. How does the last line work? that not how I would think of using the set function. Commented Mar 18, 2019 at 10:31
  • 1
    Set without an equal signs outputs all variables which match the begin.
    – user6811411
    Commented Mar 20, 2019 at 8:40
  • Can this we used with any wild cards or will it only match the beginning? I.e. if you have Vars of Number, Variable1 SecondVarrible1, Variable2 SecondVarrible2 could you call "Set %*Varible!Number!% ? Commented Mar 20, 2019 at 12:14
  • 1
    Sorry, it only matches the begin.
    – user6811411
    Commented Mar 20, 2019 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.