Writing command line scripts, or .bat files in microsoft speak, can be very worthwhile to make repetitive tasks easier. However, contrary to the linux world where writing scripts and batch tasks is common, this is not so mainstream in the windows world, hence there is also less documentation to be found about it.
Yesterday, we had a problem with trying to create a batch file which writes another batch file.
Specifically, we needed to output the pipe character into a file.
Considering that following works
echo whatever we need to output >output.txt
We also assumed we could write
echo type input.txt | blabla >output.txt
Quote naturally this tries to echo “type input.txt” to “blabla>output.txt” which gives an error as “blabla” is an iunknown command.
Our first attempt was to put quotes around the lot.
echo “type input.txt | blabla >output.txt”
This will however also echo the quotes making the result not something which can be executed.
What we needed was an escape character, which indicated that the pipe should be literal and not interpreted in the command. We tried both backslash and slash (which we thought were the obvious solutions), but they both failed.
After some hunting it appears that the caret (“^”) is the escape symbol for the windows command line (cmd.exe program in win2000/xp). If you need a literal < > | & ^ then you should prefix with a caret. So the solution to our exaple above is
echo type input.txt ^| blabla >output.txt