0

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
sweczvxcc
  • 3
  • 2
  • 2
    There's no need to save it to a multiline variable, redirect the `route` command output to a temporary file, then read it with `findstr.exe` as needed, before deleting the temporary file. _I would expect that it would be quicker too_. If you want a counter, that functionality is already built into the `findstr.exe` command, using its `/N` option. – Compo Dec 12 '20 at 19:01
  • @Compo, as stated, **a temp file is not an option**. It must be read only or it will wear out the SD card in less than 3 months.
    Note: this section will be inside a loop that checks for route changes every 20 seconds.
    – sweczvxcc Dec 12 '20 at 20:14
  • You never said that it wasn't an option at all, so please don't just try to start an argument for no good reason. There is no way whatsoever that I'd expect the read/write speed of your stated impracticably slow SD card, to be enough of an issue, to make writing to a temporary file "not an option". Do you expect that your operating system does not write anything to a file or is everything done in memory. Also, instead of just picking up the parts of my comment you wanted to argue with, please update your currently posted non batch-file code, to make it valid as I have already mentioned above. – Compo Dec 12 '20 at 21:17
  • Similarly, you should by now have put together some code using the `/N` option from `findstr.exe`, I also advised you about. This site doesn't write code for you, so in order to make your question on topic, you need to first of all have a batch file with code written to perform the task your require of it. This site will then try to assist you with a single specific, defined, and reproducible, issue resulting from your code. The code you've posted does not attempt to grab a multiline variable, nor does it attempt to parse multiple individual lines, with or without a counter. Please try harder! – Compo Dec 12 '20 at 21:26
  • Hi @Compo, I'm really sorry you took this a personal offense/argument. That wasn't definitely the intention. I didn't reply because I had that solution before and it simply wears out SD cards prematurely. I.E: in less than 3 months. Regarding the /N switch that doesn't work because the line number is expected to change continuously as different VPNs get connected/disconnected. I just need to check if some routes are present or not and if present which gateway it is using. In this case above with the /N switch I get the 5 lines as follows: # 77, 78, 79, 115 and 198. – sweczvxcc Dec 14 '20 at 15:26
  • I didn't take anything personally, because I am not a weak human being, but it appears to me so far, especially because you've still not provided us with the code which uses `findstr.exe` with the `/N` option that fails to work as needed, that you do only want to argue with the comments. We cannot help you to fix the things you're not helping us to visualize or reproduce. – Compo Dec 14 '20 at 15:33

1 Answers1

1

Your idea with numbered variables is not that bad:

@echo off
setlocal enabledelayedexpansion
REM get the output into numbered variables:
set i=1
for /f "delims=" %%a in ('route print^|findstr "127 10 192"') do (
  set /a i+=1
  set "_myvar_!i!=%%a"
)
ECHO search within those variables:
set _myvar_ |find "127"
ECHO or:
(for /f "tokens=1* delims==" %%a in ('set _myvar_') do @echo %%b) |find "127"

Note: this doesn't keep the order when i>10 (probably not a problem for you)

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • That's definitely something worth trying as it seems to accomplish my needs. Regarding the order, it's completely irrelevant as I just need to check if some route exists and if so if it has the correct gateway. If no one comes up with a better solution I'll accept your answer. Thank you! – sweczvxcc Dec 14 '20 at 15:36
  • if you replace all code after the first `for` loop (setting the variables) with 15 lines like `echo _myvar | findstr "10\.16\.0\.0.*255\.255\.0\.0.*10\.147\.1\.3" || route change 10.16.0.0 mask 255.255.255.0 10.147.1.3 metric 10`, you should be good. – Stephan Dec 14 '20 at 19:19
  • ... not `echo _myvar`, but `set _myvar` of course. – Stephan Dec 14 '20 at 19:38