Solved Extract a didgit from generated file

September 24, 2020 at 11:52:40
Specs: Windows 10
Hello, first time poster.
Not sure where to start. What I've been trying to do is extract the first didgit from the fifth line of a file generated when my computer boots. The command I'm using to do this is, route print 0.*.0 > c:\testfile.txt which gives me this....
===========================================================================
Interface List
20...00 ff ef fc 3b 71 ......TAP-Windows Adapter V9
13...02 00 bd 8f 00 cb ......Intel(R) Ethernet Connection (7) I219-V
8...02 18 ee fe ad 59 ......Intel(R) Wireless-AC 9560 160MHz
22...02 18 ee fe ad 5a ......Microsoft Wi-Fi Direct Virtual Adapter
19...00 18 ee fe ad 59 ......Microsoft Wi-Fi Direct Virtual Adapter #2
17...d4 3b 04 c5 c8 70 ......Bluetooth Device (Personal Area Network)
1...........................Software Loopback Interface 1
29...00 15 5d a7 15 4f ......Hyper-V Virtual Ethernet Adapter
48...00 15 5d da b2 0c ......Hyper-V Virtual Ethernet Adapter #2
===========================================================================

IPv4 Route Table
===========================================================================
Active Routes:
Network Destination Netmask Gateway Interface Metric
0.0.0.0 128.0.0.0 10.155.0.21 10.155.0.22 4
===========================================================================

Because the index value "8" of Intel(R) Wireless-AC 9560 160MHz changes every time I boot I've been trying to figure out how to extract that index value, in this instance "8" and use it as a variable in another batch file. Can anyone please help me. I hope this makes sense.
Thanks in advance.


See More: Extract a didgit from generated file


#1
September 24, 2020 at 21:29:17
@for /f "tokens=1 delims=." %%a in ('find "Wireless-AC 9560"^<c:\testfile.txt') do call some.bat %%a

Or, to skip a temp file:
@for /f "tokens=1 delims=." %%a in ('route print 0.*.0 ^|find "Wireless-AC 9560"') do call some.bat %%a

--- end snips
I used 'find' to circumvent absolute-line-number positional restriction on input content. "As things always change, so must we".

message edited by nbrane


Reply ↓  Report •

#2
September 25, 2020 at 00:28:10
✔ Best Answer
Give a try with this Batch code using the cmdlet Get-NetAdapter with Powershell v3

@echo off
Title Get Net Adapter Index
@for /f "tokens=1 skip=3 delims= " %%a in (
	'Powershell -C "Get-NetAdapter -Physical | Where-Object { $_.Status -eq 'Up' } | Select-Object -Property ifIndex"'
) Do (
	Set "Index=%%a"
)
echo NetAdapter Index = %Index%
pause

And if you want to get more information such Interface and MAC Address, you can try like this code below :

@echo off
Title Get Net Adapter Index,InterfaceDescription,MacAddress
@for /f "tokens=1,2,3 skip=1 delims=," %%a in (
	'Powershell -C "Get-NetAdapter -Physical | Where-Object  { $_.Status -eq 'Up' } | Select-Object -Property ifIndex,InterfaceDescription,MacAddress  | ConvertTo-Csv  -NoTypeInformation"'
) Do (
	Set "Index=%%~a"
	Set "Interface=%%~b"
	Set "MacAddress=%%~c"
)
echo( Index = %Index%
echo( Interface = %Interface%
echo( MacAddress = %MacAddress%
Pause & Exit

message edited by Hackoo


Reply ↓  Report •

#3
October 10, 2020 at 09:12:58
Thank you both @nbrane and @Hackoo for your suggestions, using your idea I was able to figure it out.
This works perfectly...

route print -4 0.*.0 > "%outfile%"

for /f "tokens=1* delims=. " %%i in ('type "%outfile%" ^| find /i "Intel(R) Wireless-AC 9560 160MHz"') do (
set index=%%i
call :processRoute
)


Reply ↓  Report •
Related Solutions


Ask Question