I need to save a filtered multi-line output into a single batch variable but so far without success.
I can't use Powershell in this stripped down version of POS Terminal and the use of a temporary file is impracticable because the OS runs from a ultra slow SD card.
With this, I need the following output in a single variable:
C:\Users\medUser> route print | findstr "127 10 192"
127.0.0.0 255.0.0.0 On-link 127.0.0.1 331
127.0.0.1 255.255.255.255 On-link 127.0.0.1 331
127.255.255.255 255.255.255.255 On-link 127.0.0.1 331
224.0.0.0 240.0.0.0 On-link 127.0.0.1 331
255.255.255.255 255.255.255.255 On-link 127.0.0.1 331
In this case, I need to capture this 5 lines (but in reality will be more than 15) as displayed into a single variable.
Is it possible without resorting to external tools?
I'm aware of some approaches like this, this, or this, but I'm unable to make it work.
I've tried the solution bellow, but unfortunately it only stores the last line.
FOR /F "tokens=* USEBACKQ" %F IN (`route print ^| findstr "127\."`) DO (SET var=%F)
ECHO %var%
Note: The end goal is to have a big string that I can parse multiple times through findstr without heaving to run the same command over and over again.
If not feasible, I'll also accept a solution where a variable has a counter, like var1
for the first line, var2
for the second, etc. Thanks!
Inefficient sample code in use:
:loop
SET _ror2="NOK"
route print | findstr "10\.16\.0\.0.*255\.255\.0\.0.*10\.147\.1\.3" && route print | findstr "192\.168\.47\.0.*255\.255\.255\.0.*192\.168\.46\.3" && route print | findstr "10\.16\.0\.0.*255\.255\.0\.0.*10\.147\.1\.3" && route print | findstr "xxxxx" && route print | findstr "repeat with 15 more conditions to test" && SET _ror2=ok
if "%_ror2%" == "ok" (
timeout 20 >NUL
goto loop
)
ECHO One of our 15 comparisons above wasn't found, hence re-apply routes
route change 10.16.0.0 mask 255.255.255.0 10.147.1.3 metric 10
route change 192.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
route change xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Note: this section will be inside a loop that checks for route changes every 20 seconds. – sweczvxcc Dec 12 '20 at 20:14