批处理 文件注释_批处理文件注释

批处理 文件注释

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!

好了,我们已经从基础开始,并且在前面的讨论中已经编写了我们的第一个程序。 希望你跟随他们。 如果没有,请先回过头阅读它们,然后再以批处理文件的形式进入操作员。 与所有其他脚本语言一样,批处理文件也支持分类为不同组的多种运算符。

为了启动和使用批处理文件及其中的运算符,我建议您熟悉常用的运行行命令。 有很多命令行命令。 因此,我在pastebin中创建了一个文件,其中列出了所有命令并描述了每个命令的用法。 尝试阅读所有命令,您可能会发现其中一些有用的命令。 这里是,

http://pastebin.com/te07WwWz

Batch File Comments

批处理文件注释

注释对于程序非常重要。 它们有助于提高程序的可读性。 任何程序员通常都不会从头开始,他们会修改现有版本。 因此,至关重要的是我们需要有效地注释代码。 批处理文件通常只允许单行注释。

单行注释

REM命令代表备注。 它使您可以在批处理文件中放置注释。 REM命令用于记录程序中使用的过程和功能。 这些是单行注释。 您需要在每个注释行的前面放置REM命令。 其语法如下。

REM之后的所有内容都必须忽略,因此,该命令后必须带有空格或制表符,然后跟随我们的注释。

示例 这是单行注释的示例

以下几行是多行注释

现在您已经熟悉了语法,让我们在实践中尝试一下。 我们将编写一个小程序来测试评论。

程式版本1

正如我在第一行上面所说的那样,注释是注释,而下一行则显示Hello,World !!!。 注释的问题在于,尽管它们被解释器忽略,但仍回显到命令提示符。 运行程序时,将得到如下输出。

输出 C:Usersuser1Desktop> new.bat C:Usersuser1Desktop> REM这是我的世界程序 C:Usersuser1Desktop> echo世界好! 你好,世界!!!

因此,我们将尝试将回声置于程序的第一行并修改程序。

程式版本2

运行此批处理文件时,在命令提示符下没有任何注释。 请参见下面的输出。

输出 你好,世界!!! 按任意键继续 。

但是,如果我不想在程序中关闭回显,该怎么办。 在某些情况下,您需要将echo命令保持在打开状态。 在这种情况下,注释也会在屏幕上回显。 因此,我们将尝试通过修改程序来解决该问题。

程式版本3

观察到我在REM命令之前使用了@符号,并且也将echo保持在ON状态。 因此,如果ECHO处于打开状态,并且您不想显示注释行,请在REM命令前添加一个at符号[@]。 该程序将不打印任何注释,而仅显示命令echo Hello,world !!! 然后执行结果。

程式版本4

该程序的输出与程序版本3相同,但是说明了不同类型的注释。 如果您对REM命令感到厌烦,则还可以在批处理文件中包括这些类型的注释。

指向思考

1. REM命令后必须跟空格或制表符。

2.您可以在注释中包括任何符号,没有任何限制。

3.如果ECHO处于ON状态,则注释将显示在命令提示符下。 否则,它将被忽略。

4.如果您希望ECHO为ON并且不想显示注释行,请在REM命令前使用at符号[@]。

5.您还可以通过在注释行中以两个冒号[::]开头来在批处理文件中添加注释,如下所示。

多行注释技巧

这是多行注释的基本语法。 每一行都可以替换为注释。 ENDCOMMENT是使使用GOTO跳过注释的标签。 因此,我们允许多行注释,而不必在每行之前使用REM命令。 自己尝试一下!

翻译自: https://www.thecrazyprogrammer.com/2015/06/batch-file-comments.html

批处理 文件注释

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值