Friday, May 9, 2014

HEREDOC for Windows Batch Files

From Wikipedia: "In computer science, a here document (here-document, heredoc, hereis, here-string or here-script) is a file literal or input stream literal: it is a section of a source code file that is treated as if it were a separate file."


Here's one way to accomplish this in a Windows batch file. Inside the batch file, echo the text to an output redirection, e.g. "> here.txt". Use carets "^" followed by double linebreaks to output on separate lines:

here_test.bat

echo This is my ^

multi-line here document ^

that this batch file ^

will print! > here.txt



When executed here_text.bat will create a new file here.txt containing the lines of text:

This is my
multi-line here document
that this batch file
will print!