3

When reading stdin with set /P, it seems that when reading from a pipe it does not work, while reading from a redirection it works okay. This is shown with the following lines:

D:\>echo 1 >f1

D:\>echo 2 >>f1

D:\>type f1
1
2

D:\>type f1 | (set line1=&set line2=&set /P line1=&set /P line2=&set line)
line1=1
line2=

D:\>(set line1=&set line2=&set /P line1=&set /P line2=&set line)<f1
line1=1
line2=2

D:\>

Why is line2 read in one case and not in the other?

bchab
  • 47
  • 1
  • 3
  • 1
    The difference in those scenarios is that in the first case the whole file contents is passed to the pipe and after finishing the first `set /p` the remainder of the stream is discarded. In the 2nd case only the first line is associated without discarding further input. See [this](https://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts) to better uderstand inner workings of cmd line/batch parser –  Apr 02 '18 at 13:30
  • 1
    Different question but [same answer](https://stackoverflow.com/a/41360593/2861476). – MC ND Apr 02 '18 at 18:34
  • Yes @MCND [this answer](https://stackoverflow.com/a/41360593/2861476) is indeed convincing and explains well the different behaviour using the pipe. Knowing this, the next question will be how to read the output of a pipe in a batch file? – bchab Apr 02 '18 at 20:39
  • It depends on the specific case, source of the pipe, size of the data, .... The simplest way *could be* to use a `for /f` processing the output of a command reading the pipe, something like `dir /b | for /f "delims=" %%a in ('find /v ""') do @( echo I see: %%a )` – MC ND Apr 02 '18 at 20:48
  • Indeed @MCND, but it doesn't allow to process the output of the pipe asynchronously. In your example, you will see "I see" appearing only after the dir command has completed. I was using pipes hoping to start the processing by the batch file as soon as the piped command had started producing its output – bchab Apr 02 '18 at 20:58
  • Then, in that scenario, the better approach in pure batch is to redirect the pipe into a temporary file while iterating in parallel over the temp file, reading with a input redirected `set /p` to avoid the problems exposed in the previous referenced answer. – MC ND Apr 02 '18 at 22:01
  • 1
    You can find a pure batch approach [here](https://www.dostips.com/forum/viewtopic.php?f=3&t=5386). An hybrid approach can be found [here](https://stackoverflow.com/a/10719322/2861476) – MC ND Apr 02 '18 at 22:03
  • Thanks @MCND, it really helped, this is exactly what I was looking for. – bchab Apr 03 '18 at 13:45

1 Answers1

-1

Originally posted by use MC ND in comments.

posting the same answer as is so this question is not counted in unanswered, question author also confirms in comments that the answer by MC ND was correct and it helped.

I think enough background, here is the comment as the answer.

You can find a pure batch approach here. A hybrid approach can be found hereMC ND Apr 2 at 22:03

Baljeetsingh
  • 1,865
  • 1
  • 18
  • 37