我有一个批处理文件,运行几个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来注释一些东西:

your commands here
:: commenttttttttttt

or

your commands here
REM  commenttttttttttt

 

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

your commands here      & ::  commenttttttttttt

or

your commands here      & REM  commenttttttttttt

 

注意:

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

不,普通的批处理文件使用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

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

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

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

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内联注释

例子:

@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

你可以用下面的语法在批处理文件的末尾添加注释:

@echo off
:: Start of code
...
:: End of code

(I am a comment
So I am!
This can be only at the end of batch files

只是要确保不要使用右括号。

署名:Leo Guttirez Ramirez, https://www.robvanderwoude.com/comments.php