To start and use the batch files and operators in it, I recommend you, to get familiar with generally used Run line commands. There is a very big list of Run line commands. So, I’ve created a file in pastebin that lists all the commands and describes the use of every command. Try to read all the commands, you may find some of them useful. Here it is,
Batch File Comments
Comments are very essential for a program. They help to increase the readability of the program. Any programmer mostly will not start from scratch, they will be revising the existing versions. So, it’s very essential that we need to comment the code efficiently. Batch files traditionally allow single line comments only.
Single Line Comments
The REM command stands for remark. It lets you to place a comment in a batch file. REM command is used to document the procedures and functions that you have used in your programs. These are single line comments. You need to place REM command in front of every comment line. Its syntax is a follows.
REM [Comment]
All the content after REM must be ignored so, the command must be followed by a space or tab character, then follows our comment.
Example
Here is an example for a single line comment
REM this is single line comment and lasts for one line only.
The below lines are multiple line comments
REM you know this is the first line of comment REM this is the second line. You can end here are continue as the same
Now that you are familiar with the syntax, let us try it in practice. We will write a small program to test the comments.
Program version #1
REM This is my hello, world program echo Hello, world!!
As I said above the first line is the comment and the next line prints Hello, World!!!. The problem with the comments is that though they are ignored by the interpreter, still echoed back to command prompt. When we run the program, we get the output as follows.
Output
C:Usersuser1Desktop>new.bat
C:Usersuser1Desktop>REM This is my hello, world program
C:Usersuser1Desktop>echo Hello, World!!!
Hello, World!!!
So, we will try placing the echo off on the first line of program and revise the program.
Program version #2
@echo off REM This is my hello, world program echo Hello, World!!! pause
When we run this batch file we don’t get any comments on the command prompt. See the output below.
Output
Hello, World!!!
Press any key to continue . . .
But what if I don’t want to turn echo off in my program. There are some situations where you need to keep echo command in on state. During such cases, the comments are also echoed back on the screen. So, we will try to solve it by revising the program.
Program version #3
@echo on @REM This is my Hello, world program echo Hello, World!!! pause
Observe that I have used an at @ sign before REM command and also kept echo in ON state. So, if ECHO is to be ON and you don’t want to display the comment line, prefix the REM command with an at sign [@]. This program won’t print any comments but only the command echo Hello, world!!! and then its execution result.
Program version #4
@echo on ::This is my hello, world program echo Hello, World!!! pause
This program gives the output same as the program version #3, But illustrates a different type of comments. You may also include these type of comments in your batch file if feel bored with REM command.
Points to Ponder
1. REM command must be followed by a space or tab character.
2. You may include any symbol in the comments without any restriction.
3. If ECHO is in ON state, the comment is displayed on the command prompt. Otherwise, it is ignored.
4. If you want ECHO to be ON and you don’t want to display the comment line, use an at sign [@] before REM command.
5. You can also place a comments in a batch file by starting the comment line with two colons [::] as shown below.
Trick for Multiple Line Comments
GOTO EndComment This line is comment. And so is this line. And this one... :EndComment
This is the basic syntax for multiple line comments. Every line may be replaced with comments. The ENDCOMMENT is a label that makes the comments to be skipped using GOTO. Hence we allow multiline comments without having to use REM command before each line. Try it out yourself!
@echo off GOTO Commentends Author: Crazy Programmer Date: dd/mm/yyyy Info: Depict types of comments in Batch Files. :Commentends REM Below line prints Hello, World!!! echo Hello, World!!! pause
Great explanation on proper and simple way of using comment lines with .cmd or .bat file.
If you have a remark using the double-colon (::) style inside of a FOR loop it can lead to problems, while REM never will.