rem - Comment
When writing a batch file, there are often cases where you want to add comments to make it easier to understand what the code does when you look back on it later. Additionally, if you are sharing the batch file with others, it may be helpful to add comments to explain the code’s purpose.
On this page, we explain how to add comments to batch files, from the basic usage to setting options, in an easy-to-understand way.
We also explain two types of comments: those that are only displayed when the batch file is opened as a text file, and those that are displayed on the command prompt.
Basic Usage of the rem Command
To add comments to a batch file, use the rem
command.
Here’s how to use it:
rem [comment]
For example, suppose you have the following batch file:
@echo off
setlocal
rem Store your favorite fruit in a variable
echo Please enter your favorite fruit.
set /p fruit=Example: apple
endlocal
When you run this batch file, the following will be displayed:
You can see that the comment written with the rem
command is not displayed on the command prompt, and only the content written with the echo
command is displayed.
Controlling Display on the Command Prompt
In the sample code of the batch file mentioned above, @echo off
is written at the beginning.
This is a command to control what is displayed on the command prompt for the entire batch file.
By writing this command, anything other than the content written with the echo
command will not be displayed on the command prompt.
If this command is not written, even if you wrote a comment with the rem
command, it will be displayed on the command prompt.
Another way to avoid this is to write @rem
in addition to @echo off
.
@rem
is a command that has the same function as the rem
command, but it is not displayed on the command prompt.
For example, suppose you wrote a batch file without using @echo off
or @rem
.
setlocal
@rem Store your favorite fruit in a variable
echo Please enter your favorite fruit.
set /p fruit=Example: apple
endlocal
When you run the batch file above, the following will be displayed on the command prompt:
C:\users\user>rem Store your favorite fruit in a variable
As you can see, the comment written with the rem
command is displayed on the command prompt.
Next, prepare a batch file that changes rem
to @rem
.
setlocal
@rem Store your favorite fruit in a variable
echo Please enter your favorite fruit.
set /p fruit=Example: apple
endlocal
When you run the batch file, the following will be displayed on the command prompt:
Note that the comment written with the rem
command is not displayed on the command prompt.
Be aware that if you do not write @echo off
or @rem
, comments written with the rem
command will be displayed on the command prompt.
If you encounter an error in a batch file written with the rem
command, it may be due to the character encoding.
While you can write Japanese in batch files, you need to be careful about character encoding.
If you use the commonly used character encoding UTF-8
, comments written with the rem
command may become garbled.
In some cases, errors may also occur.
Therefore, when writing in Japanese, it is recommended to set the character encoding to Shift-JIS
.