Which comment style should I use in batch files?
Which comment style should I use in batch files?
Which comment style should I use in batch files?
Re: Which comment style should I use in batch files?
**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`.
`::` 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`.