rem - Comment
Table of Contents
If you revisit a batch file later or hand it to someone else, leaving comments that explain the intent of each step makes it much easier to read.
The rem command is the official way to write comments in a Windows batch file.
This article explains the basics of rem, its relationship with @echo off and @rem, and how it differs from the commonly seen ::.
Copy-and-Paste Cheat Sheet
| Goal | Syntax |
|---|---|
| Write a one-line comment (standard) | rem comment |
| Hide the comment line from the screen | @rem comment |
| Add a comment at the end of a command line | echo hello & rem comment |
| Temporarily disable one line | Put rem at the start of the line |
| Disable several lines at once | Wrap them with goto skip_block and :skip_block (see below) |
| Quick shorthand (not recommended) | :: comment |
Basics of the rem Command
The basic syntax for a comment is:
rem comment
For example:
@echo off
setlocal
rem Ask the user for a favorite fruit
echo Please enter your favorite fruit.
set /p fruit=Example: apple
endlocal
When you run this batch file, the comment stays in the file as an explanation, and the only messages shown on screen are those printed by echo.
Why Comments Sometimes Appear on Screen
rem does not display comments.
Comments sometimes show up on screen because command echo is on, so the line itself is printed.
setlocal
rem Ask the user for a favorite fruit
echo Please enter your favorite fruit.
set /p fruit=Example: apple
endlocal
The Difference Between @echo off and @rem
@echo off
Putting @echo off at the top of a file stops the following command lines from being displayed. It is the most common way to start a batch file.
@rem
@rem suppresses command echo for that one line only.
setlocal
@rem Ask the user for a favorite fruit
echo Please enter your favorite fruit.
set /p fruit=Example: apple
endlocal
To hide unnecessary command lines for the whole batch file, put @echo off at the top. Use @rem when you only want to suppress a single line.
Writing a Comment at the End of a Line
rem is treated as a command, so to place it after another command on the same line, join them with &.
echo Starting process & rem start message
Everything after rem, including &, is treated as part of the comment. Commands placed after rem on the same line are not executed.
Is It OK to Use ::?
You will often see :: comment in batch files.
However, :: is not an official comment command. It abuses label syntax, so it can behave unexpectedly inside parenthesized blocks or when combined with redirection.
For maintainability and safety, rem is the safer choice for comments. It is especially recommended for reusable or shared batch files.
Commenting Out Code
Batch files have no official multi-line comment syntax like /* ... */ in C.
To temporarily disable several lines, adding rem to each line is the clearest and safest approach.
rem copy source.txt backup\source.txt
rem move source.txt archived\source.txt
rem echo Done
Commenting Out Multiple Lines at Once
When there are many lines, you can use goto to jump over the range.
@echo off
echo Starting
goto skip_block
echo This is never executed
copy source.txt backup\source.txt
:skip_block
echo Finished
A label written inside ( ) breaks the block’s parsing and cannot be a jump target. Inside if or for blocks, put rem on each line instead. Also keep label names unique within the file.
Character Encoding Pitfalls
rem itself does not cause errors. The usual problem is a mismatch between the batch file’s character encoding and the code page used by cmd.exe.
Batch files that contain Japanese or other non-ASCII comments can show garbled text depending on the environment.
Batch files with non-ASCII text can behave differently depending on the environment, whether saved as UTF-8 or Shift_JIS. Save in the encoding your existing setup uses, and test on the real machine, including chcp 65001 if needed.
Summary
- The official way to write a comment is
rem comment - Comments appear on screen because of command echo, not because
remprints them - Use
@echo offfor the whole file, or@remfor a single line ::is a shortcut;remis safer when robustness matters- With non-ASCII comments, check the combination of file encoding and code page