I want to write a Windows batch script foo.cmd
such that when I pipe in any text to its standard input, it writes that text to a file called foo.txt.
echo hi | foo
The above command should create a file named foo.txt and write the text hi
to it.
I tried this code in foo.cmd
.
@echo off
copy con foo.txt
But this doesn't work. When I run echo hi | foo
, the script simply waits for me to enter input via the terminal, i.e. Command Prompt window. It doesn't read the input piped it to.
How can I solve this problem?
Note that the text being piped may have multiple lines.