我有一个批处理文件,运行几个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和::之间的关键区别是:

REM本身是一个命令,而::不是。

我们可以把::作为一个令牌,只要CMD解析器在一行中遇到第一个非空白空格,它就会跳过整行,读取下一行。这就是为什么REM后面至少应该有一个空格,以便能够作为该行的注释,而::后面不需要任何空格。

REM本身是一个命令,从下面的FOR语法可以更好地理解

基本的FOR语法如下

FOR %v in (set) DO <Command> [command param] 

这里<命令>可以是任何有效的命令 因此,我们可以编写以下有效的命令行,因为rem是一个命令

FOR %i in (1,2,3) DO rem echo %i

然而,我们不能把下面的行写成::不是命令

FOR %i in (1,2,3) DO :: echo %i

注释一行

对于注释行,使用REM或::though::可能会在括号内失败

在以!<分隔符开头的延迟展开行中,>将被忽略,因此可以用于注释:

@echo off

setlocal enableDelayedExpansion

echo delayed expansion activated
!;delayed expansion commented line
echo end of the demonstration

在行末注释

对于行末的注释,您可以再次使用rem和::结合&:

echo --- &:: comment (should not be the last line in the script)
echo --- &rem comment

在文件末尾注释

因为exit命令后会被解析,你可以使用它在文件末尾添加注释:

@echo off

echo commands

exit /b 

-------------------
commnts at the end 
of the file
------------------

内联注释

不存在的变量的展开将被什么都不替换,并且设置一个变量=相当困难,你可以在内联注释中使用这个:

@echo off

echo long command %= this is a comment =% with an inline comment

多行注释

对于多行注释,可以使用GOTO(用于括号外)和REM带有条件执行(用于括号内)。详情如下:

@echo off

echo starting script

goto :end_comments
 comented line 
 one more commented line
:end_comments

echo continue with the script

(
    echo demonstration off
    rem/||(
      lines with
      comments
    )
    echo multiline comment inside
    echo brackets
)

同样的技术用宏来美化:

@echo off

::GOTO comment macro
set "[:=goto :]%%"
::brackets comment macros
set "[=rem/||(" & set "]=)"

::testing
echo not commented 1

%[:%
  multi 
  line
  comment outside of brackets
%:]%

echo not commented 2

%[:%
  second multi 
  line
  comment outside of brackets
%:]%

::GOTO macro cannot be used inside for
for %%a in (first second) do (
    echo first not commented line of the %%a execution
    %[%
        multi line
        comment
    %]%
    echo second not commented line of the %%a execution
)

在电脑速度不是很快的年代,::代替快速眼动是最好的选择。 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评论 &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