我有一个批处理文件,运行几个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作为注释。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或::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

::   commenttttttttttt
REM  commenttttttttttt

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

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

您可以使用::或rem进行注释。

注释时,使用::,因为它快3倍。这里显示了一个示例

只有当注释在if中时,才使用rem,因为冒号可能会出错,因为它们是一个标签。