Batch File Comments

Well, we have already started with the basics and already wrote our first program in the previous discussions. Hope you followed them. If not, go back and read them before you proceed into the operators in batch files.Just like all other scripting languages, batch files also support a very large variety of operators classified into different groups.

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,

http://pastebin.com/te07WwWz

 

Batch File Comments

 

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.

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

 

The below lines are multiple line comments 

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

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

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

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

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

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!

Leave a Reply

Your email address will not be published. Required fields are marked *