我有一个批处理文件,运行几个python脚本,做表修改。
我想让用户注释掉1-2个他们不想运行的python脚本,而不是从批处理文件中删除它们(这样下一个用户就知道这些脚本作为选项存在!) 我还想添加注释,以引起他们的注意,特别是他们在运行批处理文件之前需要更新的变量。我看到我可以使用REM,但它看起来更像是在用户运行后更新进度。
是否有更适当地添加注释的语法?
我有一个批处理文件,运行几个python脚本,做表修改。
我想让用户注释掉1-2个他们不想运行的python脚本,而不是从批处理文件中删除它们(这样下一个用户就知道这些脚本作为选项存在!) 我还想添加注释,以引起他们的注意,特别是他们在运行批处理文件之前需要更新的变量。我看到我可以使用REM,但它看起来更像是在用户运行后更新进度。
是否有更适当地添加注释的语法?
当前回答
多行注释
如果有大量的行你想注释掉,那么如果你能多行注释而不是每一行都注释掉会更好。
看看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...
其他回答
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评论 &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
您可以使用::或rem进行注释。
注释时,使用::,因为它快3倍。这里显示了一个示例
只有当注释在if中时,才使用rem,因为冒号可能会出错,因为它们是一个标签。
多行注释
如果有大量的行你想注释掉,那么如果你能多行注释而不是每一行都注释掉会更好。
看看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...