我有一个批处理文件,运行几个python脚本,做表修改。

我想让用户注释掉1-2个他们不想运行的python脚本,而不是从批处理文件中删除它们(这样下一个用户就知道这些脚本作为选项存在!) 我还想添加注释,以引起他们的注意,特别是他们在运行批处理文件之前需要更新的变量。我看到我可以使用REM,但它看起来更像是在用户运行后更新进度。

是否有更适当地添加注释的语法?


当前回答

The rem command is indeed for comments. It doesn't inherently update anyone after running the script. Some script authors might use it that way instead of echo, though, because by default the batch interpreter will print out each command before it's processed. Since rem commands don't do anything, it's safe to print them without side effects. To avoid printing a command, prefix it with @, or, to apply that setting throughout the program, run @echo off. (It's echo off to avoid printing further commands; the @ is to avoid printing that command prior to the echo setting taking effect.)

所以,在你的批处理文件中,你可以使用这个:

@echo off
REM To skip the following Python commands, put "REM" before them:
python foo.py
python bar.py

其他回答

您可以使用::或rem进行注释。

注释时,使用::,因为它快3倍。这里显示了一个示例

只有当注释在if中时,才使用rem,因为冒号可能会出错,因为它们是一个标签。

你可以使用::或REM来注释一些东西:

your commands here
:: commenttttttttttt

or

your commands here
REM  commenttttttttttt

 

为了在同一行执行命令,你必须添加一个&号:

your commands here      & ::  commenttttttttttt

or

your commands here      & REM  commenttttttttttt

 

注意:

在嵌套逻辑(IF-ELSE, FOR循环等)中使用::将导致错误。在这种情况下,用REM代替。

The rem command is indeed for comments. It doesn't inherently update anyone after running the script. Some script authors might use it that way instead of echo, though, because by default the batch interpreter will print out each command before it's processed. Since rem commands don't do anything, it's safe to print them without side effects. To avoid printing a command, prefix it with @, or, to apply that setting throughout the program, run @echo off. (It's echo off to avoid printing further commands; the @ is to avoid printing that command prior to the echo setting taking effect.)

所以,在你的批处理文件中,你可以使用这个:

@echo off
REM To skip the following Python commands, put "REM" before them:
python foo.py
python bar.py

这是一个古老的话题,我想在这里补充我的理解,扩展这个有趣的话题的知识。

REM和::之间的关键区别是:

REM本身是一个命令,而::不是。

我们可以把::作为一个令牌,只要CMD解析器在一行中遇到第一个非空白空格,它就会跳过整行,读取下一行。这就是为什么REM后面至少应该有一个空格,以便能够作为该行的注释,而::后面不需要任何空格。

REM本身是一个命令,从下面的FOR语法可以更好地理解

基本的FOR语法如下

FOR %v in (set) DO <Command> [command param] 

这里<命令>可以是任何有效的命令 因此,我们可以编写以下有效的命令行,因为rem是一个命令

FOR %i in (1,2,3) DO rem echo %i

然而,我们不能把下面的行写成::不是命令

FOR %i in (1,2,3) DO :: echo %i

不,普通的批处理文件使用REM作为注释。ECHO是在屏幕上打印内容的命令。

要“注释掉”文件的部分,您可以使用GOTO。所有这些命令/技术的示例:

REM it starts here the section below can be safely erased once the file is customised
ECHO Hey you need to edit this file before running it!  Check the instructions inside
ECHO Now press ctrl-c to interrupt execution or enter to continue
PAUSE
REM erase the section above once you have customised the file
python executed1.py
ECHO Skipping some stuff now
GOTO End
python skipped1.py
python skipped2.py
:END
python executed2.py

我能说什么呢?批处理文件是时代的遗迹,它们笨重而丑陋。

你可以在这个网站上阅读更多。

编辑:修改了示例,使其包含你显然正在寻找的元素。