Windows: How to Comment out a Line in a Batch File Tips, Tricks and Tutorials 27 JAN 2014

20091016-Windows-Command-PromptAs a reminder to myself so that I don’t have to Google it each and every time I need to work on a Windows Server (in other words, very seldom), there are three ways of adding comments into batch files (.bat) in Windows.

The first, most recognised way (standards-compliant, documented statement) is by making use of the REM statement. Any line in your batch file that starts with REM (which stands for REMark) is completely ignored by the batch interpreter.

Of course, because the way in which batch files are processed by COMMAND.COM and CMD.EXE, i.e. the batch file is read, the command is executed, and then the batch file is reread in order to execute the next command, rinse and repeat until the end of the file, REM statements do in essence slow down the execution of a batch file – though taking into account today’s processing power, this slowdown is negligible (unless of course your batch file is SUPER long!).

REM Comment - This bat-file moves all files

At this point it might be useful to remind yourself that by default the batch interpreter will print out each command before it is processed. Since REM commands don’t do anything, it’s safe to print them without any side effects. If however you want to avoid printing a command, including the REM statement, prefix it with @, or, to apply that setting throughout the program, run @echo off. (It’s echo off to avoid printing further commands; the @ is to avoid printing that command prior to the echo setting taking effect.)

The second way of commenting out a line in a batch file is by utilizing a clever trick that in essence has you converting your comment line into a label by prefixing it with a double colon (::). The first colon tells the batch interpreter that the following text is a label, while the second colon invalidates the label status but then forces the interpreter to still treat the line as a label anyway. This trick has the advantage of not slowing down the interpreter because it doesn’t have to stop to interpret the command statement like it has to for a REM statement – it simply jumps to the next line of the file without having to first reread the whole file again!

:: Comment - This bat-file moves all files

However, this trick does not allow you to start a comment mid line as labels always start at the first non-whitespace character in a command line. Also, code blocks are a pitfall for this trick as commands grouped by parentheses are interpreted as a single command line by the batch interpreter, which then obviously invalidates our ‘fake’ label as just mentioned above – your comment most likely won’t be sitting at the start of the concatenated command line!

Finally you can write multiple lines of comments (or comment blocks) by making use of the ability to jump over your lines of comment using the GOTO statement. In practice:

GOTO EndComment
Comment - This bat-file moves all files
Line 2 - Written by Craig
Line 3 - 2014-01-27

Note, it is probably a good idea to help others understand what is going on by using a descriptive GOTO label name.

And there you go, three different ways of adding comment lines into your .bat batch file.

Related Links:

Related Posts:

About Craig Lotter

South African software architect and developer at Touchwork. Husband to a cupcake baker and father to two little girls. I don't have time for myself any more.