Windows CMD. How do I corectly use two inner loops in FOR cycle?

Viewed 47

Code below enumerate network cards and output info in html file

rem | Write SCC

@ECHO ^<head^>^<style^> >> %~dp0%computername%.html
@ECHO .div-main {width: 100%%; height: 800px; padding: 10px; margin: 10px;} >> %~dp0%computername%.html
@ECHO .div-table {display: inline-block; width: auto; height: 790px; border-left: 1px solid black; padding: 10px; float: left; margin-top: 10px; margin-left: 10px; margin-bottom: 10px;} >> %~dp0%computername%.html
@ECHO .div-table-sec {display: inline-block; width: auto; height: 790px; border-left: 1px solid black; padding: 10px; margin-top: 10px; margin-bottom: 10px;} >> %~dp0%computername%.html
@ECHO .div-table-row {display: table-row;} >> %~dp0%computername%.html
@ECHO .div-table-cell {width: auto; padding: 10px 50px 10px; border-top: 1px solid black; border-left: 1px solid black; float: left;} >> %~dp0%computername%.html
@ECHO .div-table-cell-zero {width: auto; padding: 10px 50px 10px; border-top: 1px solid black; border-left: 1px solid black; float: left; color: red;} >> %~dp0%computername%.html
@ECHO .div-table-cell-sec {width: 256px; padding: 10px 50px 10px; border-top: 1px solid black; border-left: 1px solid black; float: left;} >> %~dp0%computername%.html
@ECHO .div-table-cell-third {width: 128px; padding: 10px 50px 10px; border-top: 1px solid black; border-left: 1px solid black; float: left;} >> %~dp0%computername%.html
@ECHO ^</head^>^</style^> >> %~dp0%computername%.html

rem | Open Main DIV
@ECHO ^<div class=^"div-main^"^> >> %~dp0%computername%.html

rem | Open table DIV
@ECHO ^<div class=^"div-table^"^> >> %~dp0%computername%.html

rem | Gather info on connections
rem | Get Connection name and its mac address
@FOR /F "skip=2 delims=, tokens=2,3" %%i IN ('wmic nic where PhysicalAdapter^=true get MACAddress^, NetConnectionID /format:csv') DO (

rem | Write Connection name as table row, as header of table
@ECHO  ^<div class=^"div-table-row^"^>%%j^</div^> >> %~dp0%computername%.html

rem | Write mac address
@ECHO ^<div class=^"div-table-row^"^>^<div class=^"div-table-cell-third^"^>MAC Address^</div^>^<div class=^"div-table-cell^"^>%%i^</div^>^</div^> >> %~dp0%computername%.html

rem | Filter connection by mac address and for each write ip address, mask ans gateway
@FOR /F "skip=2 delims=,{} tokens=2,3,4" %%a IN ('wmic nicconfig where ^(ipenabled^="true" AND macaddress^="%%i"^) get DefaultIPGateway^, IPAddress^, IPSubnet /format:csv') DO (
@ECHO ^<div class=^"div-table-row^"^>^<div class=^"div-table-cell-third^"^>IP Address^</div^>^<div class=^"div-table-cell^"^>%%b^</div^>^</div^> >> %~dp0%computername%.html
@ECHO ^<div class=^"div-table-row^"^>^<div class=^"div-table-cell-third^"^>Subnet^</div^> ^<div class=^"div-table-cell^"^>%%c^</div^>^</div^> >> %~dp0%computername%.html
@ECHO ^<div class=^"div-table-row^"^>^<div class=^"div-table-cell-third^"^>Gateway^</div^> ^<div class=^"div-table-cell^"^>%%a^</div^>^</div^> >> %~dp0%computername%.html
)
)

rem Close table DIV
@ECHO ^</div^> >> %~dp0%computername%.html

rem Close Main DIV
@ECHO ^</div^> >> %~dp0%computername%.html

pause

It works just fine, but tho only problem is IPv6 address and 64 after subnet mask (I marked it with red lines on screenshot) enter image description here The only way to get rid of those in output which I fugured out is to create two more cycle which will will take %%b and %%c as input and using ; as delimiter output only IPv4 address. But I fail to write proper code, because I can not properly escape characters. So, here I am, asking for help. Here is part of code which I change in attempt to get rid of IPv6 address

rem | Gather info on connections
rem | Get Connection name and its mac address
@FOR /F "skip=2 delims=, tokens=2,3" %%i IN ('wmic nic where PhysicalAdapter^=true get MACAddress^, NetConnectionID /format:csv') DO (

rem | Write Connection name as table row, as header of table
@ECHO  ^<div class=^"div-table-row^"^>%%j^</div^> >> %~dp0%computername%.html

rem | Write mac address
@ECHO ^<div class=^"div-table-row^"^>^<div class=^"div-table-cell-third^"^>MAC Address^</div^>^<div class=^"div-table-cell^"^>%%i^</div^>^</div^> >> %~dp0%computername%.html

rem | Filter connection by mac address and for each write ip address, mask ans gateway
@FOR /F "skip=2 delims=,{} tokens=2,3,4" %%a IN ('wmic nicconfig where ^(ipenabled^="true" AND macaddress^="%%i"^) get DefaultIPGateway^, IPAddress^, IPSubnet /format:csv') DO (

rem | Get rid of IPv6 address 
@FOR /F "delims=; tokens=1" %%z IN (%%b) DO (
@ECHO ^<div class=^"div-table-row^"^>^<div class=^"div-table-cell-third^"^>IP Address^</div^>^<div class=^"div-table-cell^"^>%%b^</div^>^</div^> >> %~dp0%computername%.html
)

@ECHO ^<div class=^"div-table-row^"^>^<div class=^"div-table-cell-third^"^>Subnet^</div^> ^<div class=^"div-table-cell^"^>%%c^</div^>^</div^> >> %~dp0%computername%.html
@ECHO ^<div class=^"div-table-row^"^>^<div class=^"div-table-cell-third^"^>Gateway^</div^> ^<div class=^"div-table-cell^"^>%%a^</div^>^</div^> >> %~dp0%computername%.html
)
)

But it reports that "Can not find file" using as filename token with IPv4 address, as shown at picture below. enter image description here

1 Answers

OK, O fugured it out. I just need to escape double quotes around %%b and %%c in inner cycle. So, final code should look like:

rem | Gather info on connections
rem | Get Connection name and its mac address
@FOR /F "skip=2 delims=, tokens=2,3" %%i IN ('wmic nic where PhysicalAdapter^=true get MACAddress^, NetConnectionID /format:csv') DO (

rem | Write Connection name as table row, as header of table
@ECHO  ^<div class=^"div-table-row^"^>%%j^</div^> >> %~dp0%computername%.html

rem | Write mac address
@ECHO ^<div class=^"div-table-row^"^>^<div class=^"div-table-cell-third^"^>MAC Address^</div^>^<div class=^"div-table-cell^"^>%%i^</div^>^</div^> >> %~dp0%computername%.html

rem | Filter connection by mac address and for each write ip address, mask ans gateway
@FOR /F "skip=2 delims=,{} tokens=2,3,4" %%a IN ('wmic nicconfig where ^(ipenabled^="true" AND macaddress^="%%i"^) get DefaultIPGateway^, IPAddress^, IPSubnet /format:csv') DO (

rem | Get rid of IPv6 address 
@FOR /F "delims=; tokens=1" %%z IN (^"%%b^") DO (
@ECHO ^<div class=^"div-table-row^"^>^<div class=^"div-table-cell-third^"^>IP Address^</div^>^<div class=^"div-table-cell^"^>%%z^</div^>^</div^> >> %~dp0%computername%.html
)

rem | Get rid of bitmask
@FOR /F "delims=; tokens=1" %%y IN (^"%%c^") DO (
@ECHO ^<div class=^"div-table-row^"^>^<div class=^"div-table-cell-third^"^>Subnet^</div^> ^<div class=^"div-table-cell^"^>%%y^</div^>^</div^> >> %~dp0%computername%.html
)

@ECHO ^<div class=^"div-table-row^"^>^<div class=^"div-table-cell-third^"^>Gateway^</div^> ^<div class=^"div-table-cell^"^>%%a^</div^>^</div^> >> %~dp0%computername%.html
)
)

pause

And produce output without IPv6 and without 64 bitmask. Cheers ;-)

Related