评论一行
对于评论行使用REM or :: 虽然:: 可能fail inside brackets
在以!<delimiter> 开头的延迟扩展行内将被忽略,因此可用于 cmets:
@echo off
setlocal enableDelayedExpansion
echo delayed expansion activated
!;delayed expansion commented line
echo end of the demonstration
行尾注释
对于行尾的 cmets,您可以再次使用 rem 和 :: 与 & 结合使用:
echo --- &:: comment (should not be the last line in the script)
echo --- &rem comment
在文件末尾注释
注意将在exit 命令之后进行解析,您可以使用它将 cmets 放在文件末尾:
@echo off
echo commands
exit /b
-------------------
commnts at the end
of the file
------------------
内联 cmets
不存在的变量的扩展被替换为空,并且用=设置变量相当困难,你可以use this for inline comments:
@echo off
echo long command %= this is a comment =% with an inline comment
多行 cmets
对于多行 cmets 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
)