Reputation: 20648
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.
Upvotes: 0
Views: 1097
Reputation: 130849
All you need is a single simple FINDSTR command. The "^"
search term is a regular expression that matches all lines of input.
@findstr "^" >foo.txt
Upvotes: 4