I've been writing some batch files, and I ran into this user guide, which has been quite informative. One thing it showed me was that lines can be commented not just with REM
, but also with ::
. It says:
Comments in batch code can be made by using a double-colon, this is better than using the REM command because labels are processed before redirection symbols.
::<remark>
causes no problems butrem <remark>
produces errors.
Why then, do most guides and examples I see use the REM
command? Does ::
work on all versions of Windows?
Another alternative is to express the comment as a variable expansion that always expands to nothing.
Variable names cannot contain
=
, except for undocumented dynamic variables like%=ExitCode%
and%=C:%
. No variable name can ever contain an=
after the 1st position. So I sometimes use the following to include comments within a parenthesized block:It is also a good method for incorporating in-line comments
The leading
=
is not necessary, but I like if for the symmetry.There are two restrictions:
1) the comment cannot contain
%
2) the comment cannot contain
:
good question... I've been looking for this functionality for long too...
after several tests and tricks it seem the better solution is the more obvious one...
--> best way I found to do it, preventing parser integrity fail, is reusing REM:
you can also use multiline with the "NULL LABEL" trick... (dont forget the ^ at the end of the line for continuity)
tl;dr:
REM
is the documented and supported way to embed comments in batch files.::
is essentially a blank label that can never be jumped to, whereasREM
is an actual command that just does nothing. In neither case (at least on Windows 7) does the presence of redirection operators cause a problem.However,
::
is known to misbehave in blocks under certain circumstances, being parsed not as a label but as some sort of drive letter. I'm a little fuzzy on where exactly but that alone is enough to make me useREM
exclusively. It's the documented and supported way to embed comments in batch files whereas::
is merely an artifact of a particular implementation.Here is an example where
::
produces a problem in aFOR
loop.This example will not work in a file called
test.bat
on your desktop:While this example will work as a comment correctly:
The problem appears to be when trying to redirect output into a file. My best guess is that it is interpreting
::
as an escaped label called:echo
.A very detailed and analytic discussion on the topic is available on THIS page
It has the example codes and the pros/cons of different options.
This page tell that using "::" will be faster under certain constraints Just a thing to consider when choosing
After I realized that I could use label
::
to make comments and comment out codeREM
just looked plain ugly to me. As has been mentioned the double-colon can cause problems when used inside()
blocked code, but I've discovered a work-around by alternating between the labels::
and:
spaceIt's not ugly like
REM
, and actually adds a little style to your code.So outside of code blocks I use
::
and inside them I alternate between::
and:
.By the way, for large hunks of comments, like in the header of your batch file, you can avoid special commands and characters completely by simply
goto
ing over your comments. This let's you use any method or style of markup you want, despite that fact that ifCMD
ever actually tried to processes those lines it'd throw a hissy.Use what ever notation you wish
*
's,@
's etc.