How to "comment-out" (add comment) in a batch/cmd?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In the world of batch programming for Windows operating systems, commenting is an essential part for scriptwriters to note the functionality of the script or to temporarily disable certain parts of the code during troubleshooting and development. The mechanisms for commenting in batch files (also often referred to as CMD files) are straightforward yet critical for efficient script management.
Understanding Comments in Batch Files
In batch files, comments are primarily used to:
- Explain and document the purpose and functionality of the script.
- Provide clarity on unpredictable or complicated parts of the code.
- Temporarily remove a line or section of code from execution without deleting it.
Single-Line Comments
The most common way to create a comment in a batch file is by using the REM command, which stands for "remark". Anything following REM on the same line will be ignored by the command interpreter during the execution of the script.
Example:
In this example, "This is a single line comment" will not affect the execution, and echo Hello, World! will display the text "Hello, World!" in the command prompt.
An alternative, but less used method is to simply start a line with double colons ::. This has the same effect as REM but is actually a label that does nothing, which some developers use for its visual neatness.
Example:
Multi-Line Comments
Batch files do not inherently support block comments, which can disable multiple lines of code at once like in other programming languages. However, you can use multiple single-line comments using REM or ::.
Example:
In this example, only "Line 3" would be printed.
Disabling Commands with Commenting Techniques
Commenting can also be used to quickly disable parts of your code. This practice is essential in debugging.
Example:
Strategic Placement of Comments
It's best practice to place comments above or to the right of the related code unless using the :: method, which should be used at the beginning of the line to prevent errors.
Example:
Efficiency Considerations
While comments are beneficial, it's important to use them effectively. Over-commenting can clutter a script making it harder to read, while under-commenting can make a script baffling to others or to oneself when returning to it after some time.
Summary Table
Here's a table summarizing key points about commenting in batch scripts:
| Method | Usage | Benefits | Considerations |
REM | Anywhere in code | Very clear as an official comment | None |
:: (double colons) | Beginning of lines | Quicker to type, visually neat | Should not be used within code blocks (like within an IF statement) |
Conclusion
Understanding how to effectively comment within batch files is essential for scripting efficiency and readability. While batch files only support single-line comments technically, using these wisely can greatly enhance the utility and maintainability of scripts. Remember, the goal is clarity and simplicity which can help both the original author and others who may need to read or modify the script later on.

