- 1 year ago
Why does set /p only get 1 line from a pipe?
The set /p
command in Windows is primarily designed to read a single line of input from the user or from a file. It is not intended to read multiple lines of input from a pipe.
When you use a pipe (|
) to redirect the output of a command to set /p
, it will only read the first line of the input. This is because set /p
expects a single line of input terminated by a newline character (\n
). Once it reads a newline character, it considers the input as complete and assigns it to the specified variable.
If you want to process multiple lines of input from a pipe, you can use a for /f
loop in combination with the set
command to read and process each line individually. Here's an example:
batch@echo off
setlocal enabledelayedexpansion
REM Process each line from the pipe
for /f "delims=" %%a in ('type input.txt') do (
set "line=%%a"
echo Processing line: !line!
REM Add your desired logic here
)
endlocal
In this example, type input.txt
is used to simulate the input from a pipe. Replace type input.txt
with the actual command or pipeline that produces the desired input. The for /f
loop iterates over each line of the input and assigns it to the line
variable. You can then perform your desired logic within the loop for each line.
By using a for /f
loop, you can process multiple lines of input from a pipe in Windows batch scripting.