Batch Script Redirection Syntax

batch filecommand line

I wrote an answer one day with a batch script example and someone pointed out that it's easier to read echo'd logic in batch scripts, when you're redirecting the output to a file, if you redirect to the file first and then put your command, text, etc. (below examples[1]).

This suggestion didn't mention any reason why you'd not want to do this though so I started investigating a little as I'm always looking for native methods to keep things looking a little cleaner.


Example Usual Way

IF EXIST "%tmpfile%" DEL /Q /F "%tmpfile%"
ECHO Some Text Here>>"%tmpfile%"
ECHO A little more text here>>"%tmpfile%"
ECHO Some other text over here man>>"%tmpfile%"
ECHO Can Scooby please have a Scooby snack>>"%tmpfile%"

Example Easier to Read Way

IF EXIST "%tmpfile%" DEL /Q /F "%tmpfile%"
ECHO>>"%tmpfile%" Some Text Here
ECHO>>"%tmpfile%" A little more text here
ECHO>>"%tmpfile%" Some other text over here man
ECHO>>"%tmpfile%" Can Scooby please have a Scooby snack

Obviously it is much easier to read logic from batch scripts with ECHO commands in this format but there would be a concern if there are any gotchas using this method as a standard in batch scripts.

I looked around on the Internet some and the most I was able to find was the one referenced source below stating to not use this technique in command lines that also contain other redirections[2].


Question

This question may be for Windows batch scripting experts or someone that has used this method for some time or that has done rigorous testing, but. . .

  1. other than the one issue (multiple redirects[2]) to not use this syntax, are there any other issues, reasons, or gotchas that you should be considered beforehand?

References

  • Redirection[1]

    • NOTES: (3)

      Redirections to one or more files tend to make batch files hard to
      read. Sometimes the lines can be padded with spaces to align all
      redirection signs and make the batch file more readable.

      However, if you were to do this with ECHO command lines, the spaces
      would really be ECHOed, which is not always convenient, to say the
      least.

      On Marc Stern's web site I found a
      great solution: just place the redirections before the actual
      commands.

      Take this imaginary batch file, for example:

      ECHO Directory of all files on C: >> LOG1.LOG

      DIR C:\ /S >> LOG1.LOG

      Not exactly easy on the eye, that one?

      How about this one, then?

      >> LOG1.LOG ECHO Directory of all files on C:

      >> LOG1.LOG DIR C:\ /S

      It will do exactly the same, no difference! Much better, isn't it? But
      now, try these:

      VER | TIME > LOG1.LOG > LOG1.LOG VER | TIME

      As you will notice, in the second line, it is the output of VER that
      gets redirected to LOG1.LOG !! As a rule of thumb: do not use this
      technique in command lines that also contain other redirections.

    source

Best Answer

I'm always looking for native methods to keep things looking a little cleaner.

Here's another alternative, that (to me) is even more readable:

(
ECHO Some Text Here
ECHO A little more text here
ECHO Some other text over here man
ECHO Can Scooby please have a Scooby snack
)>>"%tmpfile%"

It's also more maintainable, since it is much easier to turn on/off the redirection if needed as it is done in one place.

Related Question