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:
::This comment hack is not always safe within parentheses.
(
%= This comment hack is always safe, even within parentheses =%
)
It is also a good method for incorporating in-line comments
dir junk >nul 2>&1 && %= If found =% echo found || %= else =% echo not found
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 :