How to "comment-out" (add comment) in a batch/cmd?
Categories:
Mastering Comments in Batch/CMD Scripts
Learn the essential techniques for adding comments to your Batch and CMD scripts, improving readability, maintainability, and collaboration.
Commenting your code is a fundamental practice in any programming or scripting language, and Batch/CMD is no exception. Well-placed comments explain the 'why' behind your code, making scripts easier to understand, debug, and maintain for both yourself and others. This article will guide you through the various methods of adding comments in Batch and CMD, along with best practices and common pitfalls.
The REM Command: The Standard Comment
The REM
(Remark) command is the most common and straightforward way to add comments in Batch files. Any text following REM
on the same line will be ignored by the command interpreter. This makes it ideal for single-line explanations or temporarily disabling a command.
@echo off
REM This is a single-line comment
REM The script starts here
echo Hello, World!
REM echo This line is commented out and will not execute
pause
Basic usage of the REM
command for single-line comments.
REM
is widely used, it does get processed by the interpreter, which can slightly impact performance in very large scripts with many comments. For most scripts, this impact is negligible.Double Colon (::): A Faster Alternative
The double colon ::
is a popular alternative to REM
for commenting. Unlike REM
, lines starting with ::
are treated as invalid labels by the command interpreter. Since labels are typically defined with a single colon (e.g., :MyLabel
), ::
is effectively ignored, offering a slight performance advantage as the interpreter doesn't even attempt to parse it as a command. This method is often preferred for block comments or when performance is a critical concern.
@echo off
:: This is a comment using double colon
:: It's often preferred for block comments
:: or for slightly better performance.
echo This command will execute.
:: Another comment here
pause
Using ::
for comments in a Batch script.
::
inside parentheses or code blocks, as it might be interpreted differently or cause syntax errors in some contexts. Stick to REM
for safety within complex command structures.Multi-line Comments and Block Comments
Batch/CMD does not have a native multi-line comment syntax like /* ... */
in C-like languages. However, you can simulate block comments using a combination of GOTO
and a label, or by simply using multiple REM
or ::
lines.
flowchart TD A[Start Script] --> B{Comment Type?} B -->|Single Line| C[REM Command] B -->|Single Line (Fast)| D[Double Colon (::)] B -->|Block Comment| E[Simulate with GOTO/Label] C --> F[End Script] D --> F E --> F
Decision flow for choosing comment types in Batch scripts.
@echo off
REM --- Start of Block Comment Example 1 ---
REM This is the first line of a block comment.
REM This is the second line.
REM And so on...
REM --- End of Block Comment Example 1 ---
:: --- Start of Block Comment Example 2 ---
:: This is another way to create block comments.
:: Using double colons for each line.
:: It's visually distinct and often preferred.
:: --- End of Block Comment Example 2 ---
:: Simulating a multi-line comment block using GOTO
GOTO :SkipBlockComment
This line will NOT be executed.
This is part of the 'comment' block.
It can contain any text, even commands.
:SkipBlockComment
echo Script continues here after the 'comment' block.
pause
Examples of simulating multi-line comments using REM
, ::
, and GOTO
with a label.
GOTO :SkipBlockComment
method is powerful for temporarily disabling large sections of code or for including extensive documentation that you don't want the interpreter to even glance at. Just ensure your label name is unique.