Skip to main content

How to Comment Code Effectively in Batch Script (REM vs. ::)

Writing comments is one of the most important habits of a good programmer. Comments are notes in your code that are ignored by the computer but are invaluable for the humans who read it, including your future self! They explain why a piece of code exists, what a complex command does, or how to use the script. In batch scripting, there are two primary ways to create a comment: the official REM command and the unofficial but widely used double-colon (::) label trick.

This guide will explain the crucial differences between REM and ::, their respective advantages and disadvantages, and the best practices for when to use each one to make your scripts clean, readable, and easy to maintain.

What are Comments?

A comment is a line in a script that the command interpreter (cmd.exe) is instructed to ignore. Its sole purpose is to provide documentation and context for anyone reading the code.

REM This line is a comment. The command processor will not execute it.

Method 1: The Standard REM Command

The REM command, short for Remark, is the original and officially documented method for adding comments to a batch file. Any text on a line following REM is treated as a comment.

@ECHO OFF
REM This script will display a simple greeting.
SET "UserName=World"
ECHO Hello, %UserName%!

How it works: When cmd.exe encounters a line starting with REM, it recognizes it as a valid command that does nothing. It still processes the line (e.g., it expands variables if they were present), but it doesn't perform any action.

Method 2: The Unofficial :: Label Trick

The double colon (::) is not an official command. It is a clever exploitation of a syntax quirk. A single colon (:) defines a label for a GOTO command (e.g., :MyLabel). However, a label name cannot begin with a colon. Therefore, :: is an invalid label.

When the command processor's parser encounters an invalid label, it completely skips the line without processing it at all. This makes it an effective and very popular way to create comments.

@ECHO OFF
:: This script will display a simple greeting.
SET "UserName=World"
ECHO Hello, %UserName%!

This produces the exact same output as the REM example.

Key Differences: Performance and Location

While they look similar, REM and :: have one critical difference that determines when you should use them.

FeatureREM (Remark):: (Double Colon)
How it WorksIt's a real command that is processed but does nothing.It's an invalid label that is completely skipped by the parser.
PerformanceSlower. Must be read and processed on every iteration of a loop.Faster. The parser jumps over it. The performance difference is only noticeable in loops with thousands of iterations.
LocationCan be used anywhere, including inside parenthesized code blocks.CANNOT be used inside code blocks (like FOR loops or multi-line IF statements). It will break the script.

The Critical Limitation of ::

This is the most important difference. A :: comment will cause a syntax error inside a code block.

Example of script with error:

@ECHO OFF
FOR %%F IN (*.txt) DO (
:: This comment will cause the script to crash.
ECHO Found file: %%F
)

Solution: You must use REM inside a code block.

@ECHO OFF
FOR %%F IN (*.txt) DO (
REM This is the correct way to comment inside a block.
ECHO Found file: %%F
)

When to Use REM vs. :: (Best Practices)

Based on the key differences, the best practices are clear and simple:

  1. Use :: for all standard, full-line comments. It's cleaner, faster, and is the community standard for block comments, function headers, and general explanations at the main level of your script.

  2. Use REM only when you need a comment inside a parenthesized code block, such as a FOR loop or a multi-line IF statement.

Best Practices for Writing Good Comments

  • Comment the "Why," Not the "What": Good comments explain the purpose, not the obvious syntax.
    • Bad: REM Set counter to 0
    • Good: :: Reset the file counter before starting the next directory.
  • Use Block Comments: Use :: to create headers for major sections of your script to improve readability.
  • Comment Out Code: REM and :: are excellent tools for temporarily disabling a line of code for debugging.

Practical Example: A Well-Commented Script

This script uses both methods according to the best practices.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

:: ===================================================================
:: File Archiver Script
:: Author: Your Name
:: Date: 10/27/2023
::
:: This script finds all .log files in the source directory
:: and moves them to the archive, adding a date stamp.
:: ===================================================================

:: --- Configuration ---
SET "SOURCE_DIR=C:\Logs"
SET "ARCHIVE_DIR=C:\Logs\Archive"

:: --- Main Logic ---
ECHO Starting archive process...
FOR %%F IN ("%SOURCE_DIR%\*.log") DO (
REM Get the current file's name without the extension.
SET "FileName=%%~nF"

REM Create a new name with today's date. This requires delayed expansion.
SET "NewName=!FileName!_%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%.log"

ECHO Archiving "!FileName!" as "!NewName!"
MOVE "%%F" "%ARCHIVE_DIR%\!NewName!"
)

ECHO.
ECHO --- Process complete ---
ENDLOCAL

Conclusion

Both REM and :: are valid ways to add comments to a batch script, but they are not interchangeable.

  • :: (Double Colon) is faster and the modern standard for general-purpose, full-line comments at the main script level.
  • REM (Remark) is the official command and is essential for adding comments inside () code blocks, where :: will fail.

By using :: for general comments and REM for comments inside loops and IF blocks, you can write clean, efficient, and perfectly commented batch scripts.