1

It's no surprise that the official documentation doesn't really help in the matter of understanding how does the command process the result of a command instead of a filelist neither why is it even called 'FOR'. Yes I already know Stack Overflow is full of similar question but apparently, since batch scripts are influenced by so many "breaking" factors that, even as a non-batch experienced programmer, it is difficult not to get lost in the thousands exceptions and do-nots which may affect the result.

My objective, aside from learning from the best answer possible, is to formulate a generic enough question to represent the matter which is probably the most common task including the FINDSTR command:

THE QUESTION:

How do I get the output of a FINDSTR in a way that allows me to compute every result line one at the time possibly INSIDE the loop?

The most 'generic' (batch bs-proof if you know what I mean) example I can make is the following:

Let's say this is secret_file.txt

some not interesting line
            A very interesting line = "secret1";
some not interesting line
            A very interesting line = "secret2";
some not interesting line
            A very interesting line = "secret3";
some not interesting line

Now with the findstr command I can output every "secret" line like this:

findstr /R /C:"secret.\"" secret_file.txt
                                A very interesting line = "secret1";
                                A very interesting line = "secret2";
                                A very interesting line = "secret3";

But this result is just useless without further parsing right? I could have used ctrl-F over any text reader/editor for this matter, anyway, let's say I now want to output every line ONE AT THE TIME so that I can compute it, for example, saving every secret to a variable then using that variable somehow (it doesn't really matter how, we can just echo it to keep things simple).

Now, everybody agrees on the fact that for this kind of task, a FOR loop is needed. Quoting https://ss64.com/nt/for.html on the syntax, my script.bat should looks like this:

@echo off

FOR /F %%A IN ('findstr /R /C:"secret.\"" secret_file.txt') DO ECHO Batch script language is completely fine, good job Microsoft!

This just doesn't even give any output, can someone explain me why? My hypothesis was that the output from the findstr command is in a non-compatible format with the FOR command, not like I could check or something since the source is closed and the documentation doesn't even bother defining the word String. I'm ready to provide any details and even edit the question to be more visible to the wanna be Microsoft-forsaken batch scripters out there.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Patches
  • 49
  • 1
  • 8
  • Since you echo a constant text, none of the processed line(s) content is output, just 3 times your text. –  Dec 04 '18 at 13:52
  • That doesen t matter beacause not even the constant text get printed, if my objective was getting the code to work I would have had wrote something like [code] for /F "delims=" %%a in ('findstr /c:secret secret_file.txt') do echo %%a [/code], this works perfectly, but I'd like to understand why the code mentioned in the question just doesn't work. Should I escape something? – Patches Dec 04 '18 at 13:56

1 Answers1

1

Using "tokens=*" to strip off leading spaces this batch uses a counter to create a (pseudo) array secret[]

:: Q:\Test\2018\12\04\SO_53614102.cmd
@Echo off
set Cnt=0
FOR /F "tokens=*" %%A IN (
  'findstr /R /C:"secret.\"" secret_file.txt'
) DO (
  set /a Cnt+=1
  call Set Secret[%%Cnt%%]=%%A
)
Set Secret[

Sample output:

> SO_53614102.cmd
Secret[1]=A very intersting line = "secret1";
Secret[2]=A very intersting line = "secret2";
Secret[3]=A very intersting line = "secret3";

As variables in a (code block) are expanded at parse time,
delayed expansion is requiered (here through a call and doubled %%)

  • Thanks for the correction, so appearently this `for /F "delims=" %%a in ('findstr/r/c:secret secret_file.txt') do echo hi` works beacause there are no spaces other than the one required to specify the file right? Isnt this a solution too? – Patches Dec 04 '18 at 14:12
  • Nvm I replied my self, the script you posted works with findstr containing spaces too, wich shoud be somehow escaped in that case. It is pretty clear now, thanks again. – Patches Dec 04 '18 at 14:39
  • If you'd switch `tokens=*` which still uses default delims=space to `"delims="` all parsed content would also be in `%%a` but this time **with** leading spaces. –  Dec 04 '18 at 15:02