IT Questions and Answers :)

Thursday, January 16, 2020

Which of the following characters are the MOST efficient to use for a comment line when writing DOS batch files?

Which of the following characters are the MOST efficient to use for a comment line when writing DOS batch files?

  • **
  • ::
  • REM
  • ##


EXPLANATION

 :: 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.
Share:

0 comments:

Post a Comment

Popular Posts