REM

In a batch file REM at the start of a line signifies a comment or REMARK.

An alternative is adding :: at the start of a line, this has a similar effect to REM. Double colons act like a GOTO or CALL :label the second colon is invalid as a name but is still treated as a label, and so the line is always skipped.

For example:

@ECHO OFF
::
:: First comment

REM Second comment
REM

Echo Hello REM This remark is displayed by echo
Echo Hello & REM This remark is ignored by echo

Copy work.xls backup.xls &:: We backed up the file

Although you can use REM without a comment to add vertical spacing to a batch file, you can also use completely blank lines. The blank lines are ignored when processing the batch program.

The double-colon :: is not documented as a comment command. An important limitation of this is that as a label it can only be used at the beginning of the line. Also because the double colon is a command, it will be displayed if you run a batch file without @ECHO OFF

A REM comment is slightly slower to process than :: (360µs versus 58µs per comment line)
A REM comment is a documented command that may be used almost anywhere within a command line.

C:\> Echo This will work REM Echo But will this show
This will work REM Echo But will this show

You can add a comment to the end of a command line using &REM or &::
C:\> Echo This will work &:: Echo this will not show
This will work

This approach works because '&' introduces a new command on the same line.

Multi-Line Comments

One approach to adding a large multi-line block of comment test is to use plain text and a goto command to jump execution past the comments:

@Echo OFF
Goto :START
Description can go here
which can even include - | > characters

:START

The technique above will not work in the middle of a bracketed expression such as a FOR... DO(...) loop.
This is because a Goto command cannot be used inside brackets - it would break their context.

Such cases can be handled by also putting the comment text within brackets, as below:

(
  Echo the lines below are commented
  Rem/||(
    some comment text that will work within brackets.
    The REM command always evaluates to TRUE so these
    lines will never be executed.
  )
)

Comments within bracketed code blocks.

When :: labels are used as comments within a bracketed code block or FOR command, the command processor will expect (not unreasonably) that every label is followed by at least one command. (That way if you jump to the label it will have something to execute.) In fact the CMD shell will attempt to execute the second line even if it is formatted as a label.
Anything which is not a valid command, even a blank line or a closing parenthesis will cause a :: style comment to fail:

(
 Echo This example will fail
 :: some comment
)
(
:NormalLabel & Echo this will not be executed.
:SecondaryLine & Echo This will be executed.
)

When working within parentheses/brackets it is safer to use REM for all comment lines.

(
 REM something & Echo this will not be executed.
 REM somethingelse & Echo This also will not be executed.
)

However these do have to be on a separate line to any brackets:

(REM My comment & ECHO Something else)

The line above is equivalent to:
(REM
The REM wipes out everything following, including the close bracket which will typically break the script.

Bugs

A comment like the examples below will be interpreted as a command and will produce a fatal error:
::%~
or
REM %~

or similarly with a variable
set var=demo
rem %var:=
or
rem %var:*=

There errors will terminate all further batch processing and return an error message:
The following usage of the path operator in batch-parameter substitution is invalid: %~

This behaviour is a result of the batch parsing rules used by CMD.EXE and so is unlikely to ever change.

To avoid these issues, make sure your comments do not contain any % characters.

Inline comments

We have covered adding a whole line comment and adding a comment at the end of a line, but sometimes there is a need to add a small comment in the middle of a line to explain some detail.

Batch variable names can contain spaces and punctuation, we can take advantage of this fact by typing our comment as a non-existent variable. Because it doesn't exist, this variable will expand to nothing and so will have no effect when the script is run.

n.b. This only works in batch files, not directly at the command prompt.

To be sure that we don't accidently choose a name which is in fact a real variable, start and end the comment with "="

@Echo off
Echo This is an example of an %= Inline Comment =% in the middle of a line.

(Variable names starting with "=" are reserved for undocumented dynamic variables. Those dynamic variables never end with "=", so by using an "=" at both the start and end of our comment, there is no possibility of a name clash.)
Limitations: the comment text cannot contain % or :

Registry Comments

Within a registry .REG file comments can be preceded by ;

;
; Turn the NUMLOCK on at login
;
[HKEY_CURRENT_USER\Control Panel\Keyboard]
"InitialKeyboardIndicators"="2"

FTP Comments

There is no valid comment character for FTP but you can cheat by escaping to the shell and running REM
e.g.

C:\WORK>type ftpscript
!REM This is a remark
bye

C:\WORK>ftp -s:ftpscript
ftp> !REM This is a remark
ftp> bye

C:\WORK>

REM does not set or clear the Errorlevel.
REM is an internal command.

#Now stand in the place where you work, Now face West
Think about the place where you live, Wonder why you haven't before# - REM 'Stand'

Related:

In the text editor, Notepad++ you can add REM to the beginning of multiple lines: Select them and hit Ctrl-Q
Powershell: # - Comment / Remark.
Equivalent bash command: ### - Comment / Remark.


 
Copyright © 1999-2020 SS64.com
Some rights reserved