我有一个批处理文件,运行了几个修改表格的Python脚本。
我希望用户注释掉他们不想运行的1-2个Python脚本,而不是从批处理文件中删除它们(这样下一个用户就知道这些脚本存在作为选项!)
我还想添加注释,特别提醒用户在运行之前需要更新的批处理文件中的变量。我看到可以使用
REM
。但是似乎这更适用于在用户运行之后更新用户进展情况。
是否有一种语法更适合添加注释?
我有一个批处理文件,运行了几个修改表格的Python脚本。
我希望用户注释掉他们不想运行的1-2个Python脚本,而不是从批处理文件中删除它们(这样下一个用户就知道这些脚本存在作为选项!)
我还想添加注释,特别提醒用户在运行之前需要更新的批处理文件中的变量。我看到可以使用REM
。但是似乎这更适用于在用户运行之后更新用户进展情况。
是否有一种语法更适合添加注释?
::
或REM
:: commenttttttttttt
REM commenttttttttttt
&
字符:your commands here & :: commenttttttttttt
IF/ELSE
,FOR
循环等)内,::
后面应跟普通行,否则会出错(在那里使用REM
)。::
在setlocal ENABLEDELAYEDEXPANSION
内也可能失败cd "C:\Folder" :: 这会抛出语法错误
- Shaun Rowan&
。例如,打开命令提示符并运行dir ::blah
不会列出“。”的内容,请将其与dir & ::blah
进行比较,后者可以列出内容。 - Rado::
将会影响带有setlocal ENABLEDELAYEDEXPANSION
和for
的脚本。 - Azevedo::
标签作为注释可能会导致难以预测的错误,这里有一些例子。称其为“清洁”的做法有点误导人。@ATSiem - Jean-François Corbettrem
命令确实用于注释,运行脚本后它并不会自然更新任何人。一些脚本作者可能会使用它来代替echo
,因为默认情况下批处理解释器会在每个命令被处理之前打印出它。由于rem
命令不执行任何操作,因此可以安全地打印它们而不产生副作用。要避免打印命令,请在命令前面加上@
,或者为了在整个程序中都应用这个设置,请运行@echo off
。(这是echo off
以避免打印更多的命令;@
则是为了避免在设置echo生效之前打印出那个命令。)@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
- Richard Smithecho off & :: TEST
时出现错误。即使在echo
前面加上@
,它仍然会给出相同的错误,即“echo was unexpected at this time.” 是否有一种方法可以在echo off
后进行注释(换句话说是内联注释)? - Bill Joe不是的,普通的批处理文件使用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
我能说什么呢?批处理文件是很久以前的遗物,它们又臃肿又难看。
你可以在这个网站上阅读更多信息。
编辑:稍微修改了一下示例,以包含你明显正在寻找的元素。
在电脑速度不是很快的时候,人们更愿意使用 :: 而不是 REM 。 以“REM”开头的行将被读取但被忽略。以“::”开头的行完全被忽略。在“旧日子”里,这样可以加速您的代码。此外,在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
来完成这项任务。 - Rob Kennedy\n
,然后重新开始解析 [需要引用证明] - xDaizu如果您想要注释掉大量的代码行,建议使用多行注释而不是逐行注释。
请参考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...
if
而不是goto
。https://dev59.com/3Gox5IYBdhLWcg3wpFv5 http://ss64.com/vb/syntax-hybrid.html http://stackoverflow.com/a/34512715/995714 - phuclv& :: comment
color C & :: set red font color
echo IMPORTANT INFORMATION
color & :: reset the color to default
说明:
&
用于分隔两个命令,因此在这种情况下color C
是第一个命令,::设置红色字体颜色
是第二个命令。
这个带有注释的语句看起来很正确:
goto error1 :: handling the error
但这是无效的注释使用方式。它只能工作是因为goto
忽略了第一个参数以外的所有参数。证明很简单,这个goto
也不会失败:
goto error1 handling the error
但是类似的尝试
color 17 :: grey on blue
由于4个参数未知,无法执行该命令。这4个参数为::
、grey
、on
、blue
,它们不被color
命令所识别。
正确的使用方式如下:
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
。在延迟扩展的情况下,以!<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
------------------
未定义的变量会被替换为空
,由于使用=
设置变量相对困难,你可以使用这种方法进行内联注释:
@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 echo ~dp*= %~dp*
或者
%= echo ~dp*= %~dp* =%
都不起作用。将两者结合起来,最终成功了:
rem %= echo ~dp*= %~dp* =%
- hexaae%= echo ~dp*= %
视为一个变量。孤独的 ~
作为一个命令,然后用 REM 注释掉。虽然这是避免 REM %~
错误的好方法。 - npocmaka我更喜欢使用以下方式进行注释:
示例:
@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
你可以使用以下语法向批处理文件末尾添加注释:
@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