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

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

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


当前回答

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

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

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

其他回答

使用::或REM

::   commenttttttttttt
REM  commenttttttttttt

但是(正如人们所指出的):

如果它们不在行首,则添加&字符: 你的命令在这里&::commenttttttttttt 在嵌套部分(IF/ELSE, FOR循环等…)::后面应该跟着法线,否则会给出错误(在那里使用REM)。 ::在setlocal enabledelayeexpansion中也可能失败

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

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

只有当注释在if中时,才使用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

将注释与命令:use &:: comment放在同一行

color C          & :: set red font color
echo IMPORTANT INFORMATION
color            & :: reset the color to default

解释:

&分隔了两个命令,因此在本例中,color C是第一个命令,::set红色字体颜色是第二个命令。


重要的是:

这个带有注释的语句直观上看起来是正确的:

goto error1         :: handling the error

但这不是注释的有效使用。它之所以有效,只是因为goto忽略了第一个参数之后的所有参数。证明很简单,这个goto也不会失败:

goto error1 handling the error

但是类似的尝试

color 17            :: grey on blue

原因:::,grey, on, blue 4个参数与color命令不匹配,导致命令执行失败。

它只会工作:

color 17     &      :: grey on blue

所以&是不可避免的。

我更喜欢用:

REM评论 &REM内联注释

例子:

@echo off
set parameter1=%1%
REM test if the parameter 1 was received
if defined parameter1 echo The parameter 1 is %parameter1% &REM Display the parameter