我有一个批处理文件,运行几个python脚本,做表修改。
我想让用户注释掉1-2个他们不想运行的python脚本,而不是从批处理文件中删除它们(这样下一个用户就知道这些脚本作为选项存在!) 我还想添加注释,以引起他们的注意,特别是他们在运行批处理文件之前需要更新的变量。我看到我可以使用REM,但它看起来更像是在用户运行后更新进度。
是否有更适当地添加注释的语法?
我有一个批处理文件,运行几个python脚本,做表修改。
我想让用户注释掉1-2个他们不想运行的python脚本,而不是从批处理文件中删除它们(这样下一个用户就知道这些脚本作为选项存在!) 我还想添加注释,以引起他们的注意,特别是他们在运行批处理文件之前需要更新的变量。我看到我可以使用REM,但它看起来更像是在用户运行后更新进度。
是否有更适当地添加注释的语法?
当前回答
你可以用下面的语法在批处理文件的末尾添加注释:
@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
其他回答
不,普通的批处理文件使用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
我能说什么呢?批处理文件是时代的遗迹,它们笨重而丑陋。
你可以在这个网站上阅读更多。
编辑:修改了示例,使其包含你显然正在寻找的元素。
多行注释
如果有大量的行你想注释掉,那么如果你能多行注释而不是每一行都注释掉会更好。
看看Rob van der Woude在评论区发布的这篇文章:
The batch language doesn't have comment blocks, though there are ways to accomplish the effect. GOTO EndComment1 This line is comment. And so is this line. And this one... :EndComment1 You can use GOTO Label and :Label for making block comments. Or, If the comment block appears at the end of the batch file, you can write EXIT at end of code and then any number of comments for your understanding. @ECHO OFF REM Do something • • REM End of code; use GOTO:EOF instead of EXIT for Windows NT and later EXIT Start of comment block at end of batch file This line is comment. And so is this line. And this one...
我更喜欢用:
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
将注释与命令: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
所以&是不可避免的。
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