There are a number of ways to comment in a batch file
1)Using rem
This is the official way. It apparently takes longer to execute than ::
, although it apparently stops parsing early, before the carets are processed. Percent expansion happens before rem and ::
are identified, so incorrect percent usage i.e. %~
will cause errors if percents are present. Safe to use anywhere in code blocks.
2)Using labels :
, ::
or :;
etc.
For :: comment
, ': comment' is an invalid label name because it begins with an invalid character. It is okay to use a colon in the middle of a label though. If a space begins at the start of label, it is removed : label
becomes :label
. If a space or a colon appears in the middle of the label, the rest of the name is not interpreted meaning that if there are two labels :f:oo
and :f rr
, both will be interpreted as :f
and only the later defined label in the file will be jumped to. The rest of the label is effectively a comment. There are multiple alternatives to ::
, listed here. You can never goto
or call
a ::foo
label. goto :foo
and goto ::foo
will not work.
They work fine outside of code blocks but after a label in a code block, invalid or not, there has to be a valid command line. :: comment
is indeed another valid command. It interprets it as a command and not a label; the command has precedence. Which is the command to cd to the ::
volume, which will work if you have executed subst :: C:\
, otherwise you get a cannot find the volume error. That's why :;
is arguably better because it cannot be interpreted in this way, and therefore is interpreted as a label instead, which serves as the valid command. This is not recursive, i.e, the next label does not need a command after it. That's why they come in twos.
You need to provide a valid command after the label e.g. echo something
. A label in a code block has to come with at least one valid command, so the lines come in pairs of two. You will get an unexpected )
error if there is a space or a closing parenthesis on the next line. If there is a space between the two ::
lines you will get an invalid syntax error.
You can also use the caret operator in the ::
comment like so:
@echo off
echo hello
(
:;(^
this^
is^
a^
comment^
)
:;
)
:;^
this^
is^
a^
comment
:;
)
But you need the trailing :;
for the reason stated above.
@echo off
(
echo hello
:;
:; comment
:; comment
:;
)
echo hello
It is fine as long as there is an even number. This is undoubtedly the best way to comment -- with 4 lines and :;
. With :;
you don't get any errors that need to be suppressed using 2> nul
or subst :: C:\
. You could use subst :: C:\
to make the volume not found error go away but it means you will have to also put C: in the code to prevent your working directory from becoming ::\
.
To comment at the end of a line you can do
command &::
or command & rem comment
, but there still has to be an even number, like so:
@echo off
(
echo hello & :;yes
echo hello & :;yes
:;
)
echo hello
The first echo hello & :;yes
has a valid command on the next line but the second & :;yes
does not, so it needs one i.e. the :;
.
3)Using an invalid environment variable
%= comment =%
. In a batch file, environment variables that are not defined are removed from the script. This makes it possible to use them at the end of a line without using &
. It is custom to use an invalid environment variable i.e. one that contains an equals sign. The extra equals is not required but makes it look symmetrical. Also, variable names starting with "=" are reserved for undocumented dynamic variables. Those dynamic variables never end with "=", so by using an "=" at both the start and end of the comment, there is no possibility of a name clash. The comment cannot contain %
or :
.
@echo off
echo This is an example of an %= Inline Comment =% in the middle of a line.
4)As a command, redirecting stderr to nul
@echo off
(
echo hello
;this is a comment 2> nul
;this is another comment 2> nul
)
5)At the end of a file, everything after an unclosed parenthesis is a comment
@echo off
(
echo hello
)
(this is a comment
this is a comment
this is a comment
command.exe
), notcmd.exe
, the NT command processor as found on Windows 2000 onward.rem <remark>
works just fine in the latter (since at least Windows XP), andREM
is the official constracnt and the safest choice overall; while::
has its advantages, it is ultimately a hack that is problematic inside(…)
blocks (as discussed in many answers here). – mklement0