1

Consider this batch file:

@echo off

echo Separate lines:
set line=previous value
echo new value|set /p line=
set line

echo Same line:
set line=previous value
echo new value|(set /p line=&set line)

Output:

Separate lines:
line=previous value
Same line:
line=new value

Why doesn't the new value assigned to a variable on the right-hand side of a pipe "stick"?

1
  • Whilst I'm sure that your example is created just for the purpose of your question, why use echo new value|set /p line= instead of set "line=new value"? Commented Jan 24, 2018 at 14:33

1 Answer 1

1

You can't solve this with a pipe, as both sides of a pipe are executed in a new cmd.exe instance.

Therefore your sample echo new value|(set /p line=&set line) shows that the text is stored into the line variable and can be output from that instance.
But after the pipe is done, both cmd instances are destroyed and your line variable is lost.

If you want to fetch from pipe input outside your batch file you could use a FOR loop.

@echo off
setlocal EnableDelayedExpansion

set line=original
FOR /F "delims=" %%L in ('more') do set "line=%%L"
echo !line!

Test with

echo New Text | myBatch.bat
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.