1

I was trying to do something like

type test.txt | multiline.bat

with

test.txt

1
2
3

multiline.bat

set /p a=a
set /p "b=b"
set /p c=c

(echo %a% %b% %c%)>result.txt
pause

But result.txt was left with

1

when I expected

1 2 3

I found https://stackoverflow.com/a/6980605, which said

set /p doesn't work with pipes, it takes one (randomly) line from the input.

But why??

loadingnow
  • 597
  • 3
  • 20

1 Answers1

0

set /p stops reading input when it processes a newline. This can be seen with the simple script

@echo off
set /p "line=String containing newline:"
echo %line%

and then pasting the string

one
two

Only one will be displayed.


If you know how many lines you need to process, you can group your set /p commands together in a code block and redirect the file to it like this:
(
    set /p "a="
    set /p "b="
    set /p "c="
)<test.txt
(echo %a% %b% %c%)>result.txt

You could also take the name of the input file as a parameter and change test.txt to %1 in that snippet.

SomethingDark
  • 13,229
  • 5
  • 50
  • 55