Which comment style should I use in batch files?
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?
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, whereas REM
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 use REM
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 a FOR
loop.
This example will not work in a file called test.bat
on your desktop:
@echo off
for /F "delims=" %%A in ('type C:\Users\%username%\Desktop\test.bat') do (
::echo hello>C:\Users\%username%\Desktop\text.txt
)
pause
While this example will work as a comment correctly:
@echo off
for /F "delims=" %%A in ('type C:\Users\%username%\Desktop\test.bat') do (
REM echo hello>C:\Users\%username%\Desktop\text.txt
)
pause
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
.
Comments with REM
A REM
can remark a complete line, also a multiline caret at the line end, if it's not the end of the first token.
REM This is a comment, the caret is ignored^
echo This line is printed
REM This_is_a_comment_the_caret_appends_the_next_line^
echo This line is part of the remark
REM followed by some characters .:\/=
works a bit different, it doesn't comment an ampersand, so you can use it as inline comment.
echo First & REM. This is a comment & echo second
But to avoid problems with existing files like REM
, REM.bat
or REM;.bat
only a modified variant should be used.
REM^;<space>Comment
And for the character ;
is also allowed one of ;,:\/=
REM is about 6 times slower than ::
(tested on Win7SP1 with 100000 comment lines).
For a normal usage it's not important (58µs versus 360µs per comment line)
Comments with ::
A ::
always executes a line end caret.
:: This is also a comment^
echo This line is also a comment
Labels and also the comment label ::
have a special logic in parenthesis blocks.
They span always two lines SO: goto command not working.
So they are not recommended for parenthesis blocks, as they are often the cause for syntax errors.
With ECHO ON
a REM
line is shown, but not a line commented with ::
Both can't really comment out the rest of the line, so a simple %~
will cause a syntax error.
REM This comment will result in an error %~ ...
But REM is able to stop the batch parser at an early phase, even before the special character phase is done.
@echo ON
REM This caret ^ is visible
You can use &REM or &:: to add a comment to the end of command line. This approach works because '&' introduces a new command on the same line.
Comments with percent signs %= comment =%
There exists a comment style with percent signs.
In reality these are variables but they are expanded to nothing.
But the advantage is that they can be placed in the same line, even without &
.
The equal sign ensures, that such a variable can't exists.
echo Mytest
set "var=3" %= This is a comment in the same line=%
The percent style is recommended for batch macros, as it doesn't change the runtime behaviour, as the comment will be removed when the macro is defined.
set $test=(%\n%
%=Start of code=% ^
echo myMacro%\n%
)
Performance REM
vs ::
vs %= =%
In short:
-
::
and%= =%
seems to have the same performance -
REM
takes ~ 50% more time than::
- In blocks, especially loops only
REM
consumes time, but::
is removed from the cached block when the block is parsed, therefore it consumes no time
For more info see SO: Question about Comments in Batch *.bat files and speed
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 :
After I realized that I could use label ::
to make comments and comment out code REM
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 :
space
:: This, of course, does
:: not cause errors.
(
:: But
: neither
:: does
: this.
)
It'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 if CMD
ever actually tried to processes those lines it'd throw a hissy.
@echo off
goto :TopOfCode
=======================================================================
COOLCODE.BAT
Useage:
COOLCODE [/?] | [ [/a][/c:[##][a][b][c]] INPUTFILE OUTPUTFILE ]
Switches:
/? - This menu
/a - Some option
/c:## - Where ## is which line number to begin the processing at.
:a - Some optional method of processing
:b - A third option for processing
:c - A forth option
INPUTFILE - The file to process.
OUTPUTFILE - Store results here.
Notes:
Bla bla bla.
:TopOfCode
CODE
.
.
.
Use what ever notation you wish *
's, @
's etc.