100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Batch

Comments in Batch Files

How to write and use comments in batch files, and why documentation matters for long-lived scripts.

FoundationsBeginner6 min readJul 10, 2026
Analogies

Comments in Batch Files

The REM command marks a line as a comment: cmd.exe reads it and simply ignores it, moving on to the next line. A second, widely used trick is starting a line with a double colon, ::. This works because a line beginning with a colon is treated as a label, and labels are never executed during normal top-to-bottom script flow -- they're only reached if a GOTO explicitly jumps to them -- so :: effectively behaves like a comment in most contexts.

🏏

Cricket analogy: REM in a batch file is like a coach's handwritten note in the margin of the match plan -- everyone reading the sheet sees it, but the players executing the plan skip right over it.

REM vs :: -- Behavior and Pitfalls

The :: trick has a real pitfall: inside a parenthesized block, such as an IF (...) or a FOR (...) do (...) construct, cmd.exe's block parser can misinterpret :: as an invalid label and throw an error like 'was unexpected at this time.' REM does not have this problem -- it is always syntactically safe, in any position, including deep inside nested blocks. The only downsides of REM are that it's marginally slower to process and, unlike ::, its text is displayed if command echoing (ECHO ON) is active.

🏏

Cricket analogy: Using :: inside a FOR block is like a substitute signal that works fine on the boundary rope but causes total confusion if used mid-delivery inside the bowler's run-up -- context changes how it's interpreted.

Commenting Style and Best Practices

Good batch scripts use a consistent REM header block at the top: what the script does, who wrote it, what parameters it expects, and when it was last updated. This matters because batch has none of the tooling modern languages take for granted -- no docstrings, no IntelliSense hover text, no auto-generated documentation. Comments are the only place that context lives, so they should explain why a step exists or what edge case it handles, not just restate what the command obviously does.

🏏

Cricket analogy: A REM header block describing a script's purpose is like the team sheet posted before a match, listing the game plan, conditions, and personnel up front so anyone reading it later understands the strategy.

batch
@echo off
REM ============================================
REM Script:  cleanup.bat
REM Purpose: Deletes temporary .tmp files
REM Author:  IT Automation Team
REM Date:    2026-07-10
REM ============================================

REM This is a safe comment anywhere in the script.
del /q *.tmp

:: This pseudo-comment is fine here, at the top level...
for /l %%i in (1,1,3) do (
    REM ...but a "::" comment inside a parenthesized block
    REM can throw "was unexpected at this time" errors.
    REM Use REM instead of :: inside blocks to stay safe.
    echo Pass %%i complete
)

Avoid using :: for comments inside any parenthesized block, such as IF (...) or FOR (...) do (...). Because :: is really an unreachable label, cmd.exe's block parser can misread it and throw a 'was unexpected at this time' error. REM is always safe in every position, including inside blocks.

Why Comments Matter for Maintenance

In enterprise environments, batch scripts have a habit of outliving the person who wrote them by years, sometimes getting quietly copied across servers and modified by several different administrators over time. Without comments, a later maintainer has to reverse-engineer intent purely from the raw commands -- was that DEL command there to clean up temp files, or is it silently relying on a side effect something else depends on? A little documentation up front saves a lot of risky guesswork down the line.

🏏

Cricket analogy: An undocumented batch script outliving its author is like an old-school scoring method passed down verbally through generations of scorers -- without written notes, each new scorer risks misreading the original intent.

  • REM marks a line as a comment; cmd.exe skips it entirely during execution.
  • :: is a widely used pseudo-comment trick that works because a label line is never executed unless jumped to.
  • :: can break inside parenthesized IF or FOR blocks, causing parser errors; REM does not have this problem.
  • REM is slightly slower than :: and is displayed if ECHO is ON, but it is always syntactically safe.
  • Use a consistent REM header block at the top of scripts to document purpose, author, parameters, and date.
  • Avoid appending REM directly after a command on the same line without proper separation.
  • Good comments explain why a step exists, not just restate what the command obviously does.

Practice what you learned

Was this page helpful?

Topics covered

#Batch#WindowsBatchScriptingStudyNotes#MicrosoftTechnologies#CommentsInBatchFiles#Comments#Files#REM#Behavior#StudyNotes#SkillVeris