我有一个批处理文件,运行几个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...

其他回答

在电脑速度不是很快的年代,::代替快速眼动是最好的选择。 REM'ed行被读取,然后忽略。:“埃德一行都被忽略了。”这可以在“过去”加速你的代码。而且在快速眼动之后你需要一个空间,在::之后你不需要。

正如第一条评论所说:你可以在任何你觉得需要的行中添加信息

SET DATETIME=%DTS:~0,8%-%DTS:~8,6% ::Makes YYYYMMDD-HHMMSS

至于跳过的部分。 把REM放在每一行的前面是相当耗时的。 如前所述,使用GOTO跳过部分是跳过大块代码的一种简单方法。确保在希望代码继续的位置设置:LABEL。

SOME CODE

GOTO LABEL  ::REM OUT THIS LINE TO EXECUTE THE CODE BETWEEN THIS GOTO AND :LABEL

SOME CODE TO SKIP
.
LAST LINE OF CODE TO SKIP

:LABEL
CODE TO EXECUTE

使用::或REM

::   commenttttttttttt
REM  commenttttttttttt

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

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

你可以使用::或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内联注释

例子:

@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