Batch Script Redirection Syntax

4

3

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

Pimp Juice IT

Posted 2016-07-31T01:40:14.113

Reputation: 29 425

Why are you deleting "%tmpfile%" immediately after writing to it? You might as well not bother with the echo's :) – DavidPostill – 2016-07-31T09:33:09.070

@DavidPostill I took the other logic out of the examples that would run before that but good point. – Pimp Juice IT – 2016-07-31T17:06:42.613

1Please note that "easier to read" is not related to "easier to figure out". What you described works, but it is a pretty obscure feature, I wouldn't use it because it will likely confuse other people reading your script. Other than that and what you already pointed out I know no other problems with it. I'd recommend @David Postrill 's answer, failing that - just align text with TABs or spaces - that's not a great solution but at least not a confusing one. – Jack White – 2016-07-31T19:57:15.440

Answers

6

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.

DavidPostill

Posted 2016-07-31T01:40:14.113

Reputation: 118 938