我有一个批处理文件,它运行几个执行表修改的 python 脚本。
我想让用户注释掉他们不想运行的 1-2 个 python 脚本,而不是从批处理文件中删除它们(所以下一个用户知道这些脚本作为选项存在!)我还想添加注释到在运行批处理文件之前,请特别提请他们注意他们需要在批处理文件中更新的变量。我看到我可以使用 REM。但它看起来更像是在用户运行后更新用户的进度。
是否有更恰当地添加评论的语法?
Rem Sth
命令或使用此标记::: Sth
使用 :: 或 REM
:: commenttttttttttt
REM commenttttttttttt
但是(正如人们所说):
如果它们不在行首,则添加 & 字符:您的命令在这里 &::commenttttttttttt
内部嵌套部分(IF/ELSE、FOR 循环等):: 后面应该跟普通行,否则会出错(在此处使用 REM)。
:: 也可能在 setlocal ENABLEDELAYEDEXPANSION 内失败
rem
命令确实用于评论。运行脚本后,它本质上不会更新任何人。但是,一些脚本作者可能会以这种方式使用它而不是 echo
,因为默认情况下,批处理解释器会在处理之前打印出每个命令。由于 rem
命令不执行任何操作,因此可以安全地打印它们而不会产生副作用。要避免打印命令,请在其前面加上 @
,或者,要在整个程序中应用该设置,请运行 @echo off
。 (echo off
是避免打印更多命令;@
是避免在 echo 设置生效之前打印 that 命令。)
所以,在你的批处理文件中,你可以使用这个:
@echo off
REM To skip the following Python commands, put "REM" before them:
python foo.py
python bar.py
REM This whole line is a comment
@echo off :: This comment is inline
#
的注释,因为 #
显然可能不是在 Configure 旨在支持的 所有 shell 中的有效注释指示符;见第 3 行。
不,普通的旧批处理文件使用 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
我能说什么?批处理文件是很久以前的遗物,它们笨重且丑陋。
您可以阅读更多on this website。
编辑:稍微修改了示例以使其包含您显然正在寻找的元素。
在计算机不是很快的日子里,最好使用 :: 而不是 REM。 REM'ed 行被读取,然后被忽略。 ::'ed 行一直被忽略。这可以在“过去”加速您的代码。此外,在 REM 之后你需要一个空格,在 :: 你不需要之后。
正如第一条评论中所说:您可以将信息添加到您认为需要的任何行
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
同等地做到这一点。
\n
然后再次开始解析 [需要引用]
多行注释
如果您要注释掉大量行,那么最好进行多行注释而不是注释掉每一行。
请参阅this post by Rob van der Woude on comment blocks:
批处理语言没有注释块,尽管有一些方法可以实现效果。 GOTO EndComment1 这一行是注释。这条线也是如此。而这个... :EndComment1 您可以使用 GOTO Label 和 :Label 进行块注释。或者,如果注释块出现在批处理文件的末尾,您可以在代码末尾写 EXIT,然后添加任意数量的注释以供您理解。 @ECHO OFF REM 做某事 • • REM 代码结束;使用 GOTO:EOF 代替 EXIT 用于 Windows NT 和更高版本 EXIT 批处理文件末尾的注释块开始 此行是注释。这条线也是如此。和这个...
if
而不是 goto
stackoverflow.com/q/9074476/995714 ss64.com/vb/syntax-hybrid.html stackoverflow.com/a/34512715/995714
将注释与命令放在同一行:使用 &::comment
color C & :: set red font color
echo IMPORTANT INFORMATION
color & :: reset the color to default
解释:
&
separates two commands,因此在这种情况下,color C
是第一个命令,:: set red font color
是第二个命令。
重要的:
这个带有注释的语句在直觉上看起来是正确的:
goto error1 :: handling the error
但这不是评论的无效使用。它之所以有效,只是因为 goto
忽略了第一个参数之后的所有参数。证明很简单,这个 goto
也不会失败:
goto error1 handling the error
但类似的尝试
color 17 :: grey on blue
由于 color
命令的 4 个参数未知:::
、grey
、on
、blue
,因此执行命令失败。
它只能作为:
color 17 & :: grey on blue
所以&符号是不可避免的。
您可以使用 ::
或 REM
注释掉某些内容:
your commands here
:: commenttttttttttt
或者
your commands here
REM commenttttttttttt
要在与命令相同的行上执行此操作,您必须添加一个 & 符号:
your commands here & :: commenttttttttttt
或者
your commands here & REM commenttttttttttt
笔记:
在嵌套逻辑(IF-ELSE、FOR 循环等)中使用 :: 会导致错误。在这些情况下,请改用 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
只要确保你从不使用右括号。
署名:https://www.robvanderwoude.com/comments.php 上的 Leo Guttirez Ramirez
这是一个老话题,我想在这里补充一下我的理解,以扩展这个有趣话题的知识。
REM 和 :: 之间的主要区别是:
REM 本身就是一个命令,而 :: 不是。
我们可以将 :: 视为一个标记,一旦 CMD 解析器在一行中遇到第一个非空格是这个 :: 标记,它就会跳过整行并读取下一行。这就是为什么 REM 后面应该至少有一个空格才能作为该行的注释,而 :: 后面不需要任何空格。
REM 本身就是一个命令,可以从以下 FOR 语法中得到最好的理解
基本的 FOR 语法如下
FOR %v in (set) DO <Command> [command param]
这里 <Command>
可以是任何有效的命令所以我们可以编写以下有效的命令行,因为 rem
是一个命令
FOR %i in (1,2,3) DO rem echo %i
但是,我们不能写以下行,因为 ::
不是命令
FOR %i in (1,2,3) DO :: echo %i
::
是一个(无效)标签(解释了它的行为),因此不应使用(尽管它仍然很常见)
评论一行
对于评论行使用 REM or :: 虽然 ::
可能 fail inside brackets
在以 !<delimiter>
开头的延迟扩展行内将被忽略,因此可用于注释:
@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
------------------
内联评论
不存在的变量的扩展被替换为空,并且使用 =
设置变量相当困难,您可以use this for inline comments:
@echo off
echo long command %= this is a comment =% with an inline comment
多行注释
对于多行注释,可以使用 GOTO
(用于外括号)和带条件执行的 REM
(用于内括号)。 More details here:
@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 echo ~dp*= %~dp*
或 %= echo ~dp*= %~dp* =%
不起作用。将两者结合起来终于奏效了:rem %= echo ~dp*= %~dp* =%
%= echo ~dp*= %
作为变量。孤独的 ~
作为命令,然后用 REM 注释。虽然是避免 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
cd "C:\Folder" ::this throws a syntax error
::
之间添加&
。为了说明,打开 cmd 提示符并运行dir ::blah
,它没有列出.
的内容,并与dir & ::blah
比较,它确实::
会使带有setlocal ENABLEDELAYEDEXPANSION
和for
的脚本出错::
标签作为评论会如何导致难以预料的错误。称这个干净有点误导@ATSiem