Batch Programming
CMD Commands
Code Commenting
Script Editing
Coding Tips

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 Windows batch files, use REM to add a comment. Everything after REM on the same line is ignored by the command interpreter. The alternative is :: (double colon), which is technically a label that goes nowhere but is widely used as a comment because it is shorter and visually cleaner. Both approaches are single-line only. Batch has no native block comment syntax.

REM: The Official Comment Command

REM (short for "remark") is the documented, supported way to add comments in batch files. The command interpreter skips everything after REM on the same line.

bat
1@echo off
2REM This script backs up the database before deployment
3REM Author: deploy-team
4REM Last updated: 2026-06-01
5
6echo Starting backup...

REM works everywhere in a batch file: at the top level, inside IF blocks, inside FOR loops, and inside parenthesized code blocks. This universality is its primary advantage over ::.

Inline comments with REM

You can place REM after a command using &:

bat
set DB_HOST=localhost & REM Database host for local development
set DB_PORT=5432 & REM Default PostgreSQL port

The & chains two commands on one line. The second command is REM, which does nothing. This is the closest batch scripting gets to an inline comment.

:: (Double Colon): The Practical Alternative

:: is technically a label with an invalid name. Because the interpreter cannot jump to it, it effectively does nothing. Developers adopted it as a comment syntax because it is shorter than REM and visually stands out more.

bat
1@echo off
2:: =============================================
3:: Deploy script for production environment
4:: =============================================
5
6echo Deploying to production...

The visual clarity is real. In scripts with heavy commenting, :: creates a cleaner look than REM because it uses fewer characters and creates a more consistent left margin.

REM vs :: Comparison

FeatureREM::
Official documentationYes, supported commandNo, exploits label syntax
Works at top levelYesYes
Works inside IF/FOR blocksYesUnreliable (can cause errors)
Works inside parenthesized groupsYesCan break the block
Inline after a commandYes, with & REMNo
PerformanceSlightly slower (parsed as command)Slightly faster (skipped as label)
Visual clarityModerateHigh

The performance difference is negligible in practice. The real decision point is whether your comment appears inside a code block.

The :: Problem Inside Code Blocks

This is the most important thing to know about :: in batch scripts. Inside parenthesized blocks (which includes IF/ELSE, FOR, and multi-line grouped commands), :: can cause syntax errors or unpredictable behavior.

This works:

bat
1@echo off
2IF "%1"=="deploy" (
3    REM Run the deployment
4    echo Deploying...
5    echo Done.
6)

This can break:

bat
1@echo off
2IF "%1"=="deploy" (
3    :: Run the deployment
4    echo Deploying...
5    echo Done.
6)

The interpreter sometimes treats :: inside parenthesized blocks as a malformed label definition, which disrupts the parsing of the block. The behavior varies across Windows versions and depends on surrounding code. The safe rule: always use REM inside parenthesized blocks.

Commenting Out Code for Debugging

When you need to temporarily disable lines during troubleshooting, prefix them with REM:

bat
1@echo off
2echo Step 1: Preparing files...
3REM echo Step 2: Uploading to server...
4REM echo Step 3: Running migrations...
5echo Step 4: Sending notification...

Only Step 1 and Step 4 execute. Steps 2 and 3 are commented out but preserved in the script for easy re-enabling.

For multiple consecutive lines, REM is the only reliable approach:

bat
1@echo off
2REM ---- Temporarily disabled for debugging ----
3REM xcopy /s /y "C:\app\build" "\\server\deploy\"
4REM net stop MyService
5REM net start MyService
6REM ---- End disabled section ----
7echo Debug mode: skipping deployment steps.

Simulating Block Comments With GOTO

Batch has no multi-line comment syntax, but you can simulate it with a GOTO that skips a section:

bat
1@echo off
2echo Before the comment block.
3
4goto :skip_comment
5This entire block is skipped.
6You can write anything here.
7No REM prefix needed.
8Even special characters like | > < work.
9:skip_comment
10
11echo After the comment block.

The GOTO jumps to the :skip_comment label, bypassing everything in between. This is useful for temporarily disabling large sections of a script without adding REM to every line.

A cleaner version uses a descriptive label:

bat
1goto :end_old_logic
2REM Old backup logic - replaced 2026-06-01
3xcopy /s /y "%SOURCE%" "%DEST%"
4if errorlevel 1 echo Backup failed
5:end_old_logic

Using % Comments in Special Cases

Some developers use a variable expansion trick for end-of-line comments:

bat
set count=5 %= This sets the retry count =%

The %= and =% delimiters create an undefined variable name containing a space, which expands to nothing. This is a hack, not official syntax, and it fails if EnableDelayedExpansion interacts badly with the content. It is mentioned here for completeness, but & REM is more readable and reliable.

Best Practices for Batch File Comments

Script headers

Start every non-trivial batch file with a header block:

bat
1@echo off
2REM ================================================================
3REM Script:  deploy.bat
4REM Purpose: Deploy application to staging environment
5REM Usage:   deploy.bat [environment] [version]
6REM Example: deploy.bat staging 2.1.0
7REM ================================================================

Section markers

Use comment blocks to separate logical sections:

bat
1REM ---- Configuration ----
2set APP_NAME=myapp
3set DEPLOY_DIR=C:\deploy\%APP_NAME%
4
5REM ---- Validation ----
6if "%1"=="" (
7    echo Error: Environment argument required.
8    exit /b 1
9)
10
11REM ---- Deployment ----
12echo Deploying %APP_NAME% to %1...
13xcopy /s /y "build\*" "%DEPLOY_DIR%\"

Explain the "why," not the "what"

bat
1REM Bad: Set the timeout to 30
2set TIMEOUT=30
3
4REM Good: 30 seconds allows the database connection pool to drain fully
5set TIMEOUT=30

Common Pitfalls

Using :: inside IF, FOR, or parenthesized blocks. This is the most common batch commenting mistake. The interpreter can misparse :: as a label inside these constructs, producing cryptic errors. Always use REM inside code blocks.

Assuming batch supports block comments. Developers coming from C, Java, or Python expect /* */ or """ syntax. Batch has no equivalent. Use repeated REM lines or the GOTO skip pattern.

Forgetting & before inline REM. Writing set X=5 REM comment does not create a comment. It sets X to 5 REM comment. You need set X=5 & REM comment with the & command separator.

Over-commenting obvious code. Adding REM increment counter above set /a counter+=1 adds noise without value. Comment on the purpose and context, not the mechanics.

Leaving debug REM lines in production scripts. Commented-out code that stays in a script indefinitely creates confusion about whether it should be re-enabled or deleted. Clean up disabled code once the debugging session is complete.

Summary

  • REM is the standard, universally safe comment command in batch files. Use it by default, especially inside code blocks.
  • :: is a popular shorthand that is visually cleaner but breaks inside parenthesized blocks (IF, FOR, grouped commands).
  • For inline comments after a command, use & REM comment text.
  • Simulate block comments with GOTO :label to skip large sections without prefixing every line.
  • Always use REM inside IF/ELSE and FOR loops to avoid parsing errors.
  • Comment on purpose and context rather than restating what the code literally does.

Course illustration
Course illustration

All Rights Reserved.