Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 but rem <remark> produces errors.

Why then, do most guides and examples I see use the REM command? Does :: work on all versions of Windows?

like image 799
MikeFHay Avatar asked Sep 13 '12 13:09

MikeFHay


People also ask

How do you comment in a batch file?

A batch file can be commented using either two colons :: or a REM command. The main difference is that the lines commented out using the REM command will be displayed during execution of the batch file (can be avoided by setting @echo off ) while the lines commented out using :: , won't be printed.

What does %% mean in batch files?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

Which of the following is used to put a comment in a batch file?

Comments Using the :: Statement The other way to create comments in Batch Script is via the :: command. Any text which follows the :: statement will be treated as comments and will not be executed.

Can you comment out lines in a batch file?

The batch language doesn't have comment blocks, though there are ways to accomplish the effect. You can use GOTO Label and :Label for making block comments. Or, If the comment block appears at the end of the batch file, you can write EXIT at end of code and then any number of comments for your understanding.


2 Answers

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.

like image 79
Joey Avatar answered Nov 16 '22 01:11

Joey


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

like image 23
jeb Avatar answered Nov 16 '22 01:11

jeb