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
.