Batch File Comment: How to Add Remarks in .bat Scripts
To add a comment in a Windows batch file, use the REM command followed by your text, or use the double-colon :: shorthand. Both tell the command interpreter to ignore the rest of the line, letting you document your script without affecting execution.
What Is a Batch File Comment?
A batch file comment is a line of text that the command processor (cmd.exe) does not execute. It exists purely for human readers—you or someone else who opens the .bat file later—to understand what the script does, why a particular command is there, or to temporarily disable a line of code. Comments are not stored in a separate metadata field; they live inside the script itself, usually on their own lines.
There are two primary ways to write a comment in a batch file: REM and ::. Both are recognized by cmd.exe, but they behave slightly differently in certain edge cases, which we’ll cover below.
Using the REM Command
REM (short for “remark”) is the original and most portable comment command. It works in every version of Windows, from MS-DOS through Windows 11, and also in batch files run under compatibility layers like Wine. To use it, type REM followed by a space and then your comment text:
REM This script backs up the Documents folder REM Created on 2024-01-15
You can also put REM on the same line as a command, but only after an ampersand (&), not after the command itself:
copy file1.txt file2.txt & REM This copies the file
One important detail: REM is a command, so it requires a space after it. Writing REMcomment will cause an error because the interpreter tries to run a command named “REMcomment”.
Using the Double-Colon (::) Shorthand
The double-colon :: is a less official but widely used alternative. It looks like a label (which batch files use with GOTO), but because it starts with a colon, it’s treated as a label that doesn’t exist—so the interpreter skips it. This makes it a quick way to add a comment without typing REM:
:: This is a comment :: Another comment line
Why use :: over REM? It’s slightly faster because the interpreter doesn’t have to parse a command; it just skips the line. In practice, the speed difference is negligible for normal scripts, but in tight loops with thousands of iterations, it can add up. Also, some programmers find it visually cleaner.
However, :: has a known limitation: it can break inside parenthesized blocks (like IF or FOR loops) in certain older Windows versions. For example:
IF EXIST file.txt (
:: This comment can cause an error
ECHO Found
)
In some Windows 7 and earlier environments, this would throw an error because the parser treats :: as an invalid label inside a block. REM does not have this problem. For maximum compatibility, especially in scripts with complex control flow, stick with REM.
How to Choose Between REM and ::
The choice depends on your priorities:
- Portability and safety: Use
REM. It works everywhere, including inside parenthesized blocks, and is the officially documented command. - Speed and brevity: Use
::for simple, top-level comments where you want to save a few characters and avoid the extra command parsing. - Consistency: Pick one and use it throughout a script. Mixing them is fine but can confuse readers.
If you’re writing a script that might be run on older Windows versions or under non-standard shells, REM is the safer bet. For modern Windows 10/11 scripts, :: is generally fine except inside parenthesized blocks—where you should use REM anyway.
Common Mistakes and Gotchas
Here are the pitfalls most batch file authors run into:
- No space after REM:
REMcommentfails. Always put a space. - Using :: inside a parenthesized block: As noted, this can cause “The system cannot find the batch label specified” errors in some versions. Use
REMthere. - Special characters in comments:
REMlines are parsed for metacharacters like>,|, and&. If your comment contains them, you need to escape them with a caret (^) or the line may be misinterpreted. For example,REM Use > to redirectshould be written asREM Use ^> to redirect. - Trailing spaces: A comment line with trailing spaces is fine, but if you accidentally put a space before
REM, it’s still fine (the interpreter ignores leading spaces). However, a space before::is also ignored, so that’s not an issue. - Commenting out a command: To temporarily disable a command, put
REMor::at the start of the line. But be careful: if the command spans multiple lines (using caret^for line continuation), you must comment out each line individually.
Comments vs. Labels vs. Echo
New users sometimes confuse comments with two other constructs:
- Labels: A line starting with a single colon (e.g.,
:mylabel) is a target forGOTO. It is not a comment—it’s executable control flow.::is technically a label that doesn’t exist, which is why it’s skipped. - ECHO:
ECHO textprints text to the screen. It’s not a comment; it’s an output command. If you want the user to see a message, useECHO. If you want to leave a note for yourself, useREMor::.
A common pattern is to combine ECHO with REM to show a message only in debug mode:
REM Debug mode: set DEBUG=1 to see extra output IF "%DEBUG%"=="1" ECHO Processing file %1
Frequently Asked Questions
Can I put a comment on the same line as a command?
Yes, but only after an ampersand (&). For example, dir & REM List files works. You cannot put a comment after a command without the ampersand because the interpreter treats the rest of the line as arguments to the command. If you need a comment on the same line, the ampersand is the only way.
Does REM work in all Windows versions?
Yes, REM has been part of the command interpreter since MS-DOS and works in every version of Windows, including Windows 11. It’s the most compatible way to comment.
Why does my :: comment cause an error inside an IF block?
In certain Windows versions, the parser mishandles :: inside parenthesized blocks because it expects a valid label. The error message is usually “The system cannot find the batch label specified.” The fix is to replace :: with REM inside any block that uses parentheses.
Can I comment out multiple lines at once?
There’s no block comment syntax in batch files. You must put REM or :: at the start of each line. Some editors allow you to select multiple lines and add the prefix automatically, but the script itself has no block comment feature.
Is there a way to make comments appear in the console output?
No, comments are invisible to the user. If you want to display a message, use ECHO instead. Comments are only for reading the source code.
Can I use // or # for comments like in other languages?
No. The command interpreter only recognizes REM and ::. Using // or # will cause an error because the interpreter will try to execute them as commands.
Best Practices for Commenting
Good comments make a batch file maintainable. Follow these simple rules:
- Add a header comment at the top of the file with the script’s purpose, author, and date.
- Comment non-obvious logic, especially complex
IFconditions orFORloops. - Use
REMinside parenthesized blocks; use either style at the top level. - Keep comments concise and current—stale comments are worse than none.
- Escape special characters in comments that might confuse the parser.
By following these practices, you’ll write batch files that are easier to debug, share, and update months later.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.