rem - Comment

Maintained on

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:

sample.cmd
@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:

×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>sample.cmd
Please enter your favorite fruit.
Example: apple

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.

sample.cmd
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:

×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>sample.cmd
C:\users\user>setlocal

C:\users\user>rem Store your favorite fruit in a variable

C:\users\user>echo Please enter your favorite fruit.
Example: apple
C:\users\user>set /p fruit=Example: apple
Example: apple

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.

sample.cmd
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:

×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>sample.cmd
C:\users\user>setlocal
C:\users\user>echo Please enter your favorite fruit.
Please enter your favorite fruit.
C:\users\user>set /p fruit=Example: apple
Example: apple

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.

Confirmation Test

Practice Problem

Which of the following will not be displayed on the screen when executing a batch file with @echo on written at the beginning of the file?

回答がサーバーに送信されることはありません
#PowerShell #Command Prompt #Batch Files