Commenting a line
For commenting line use REM or :: though ::
might fail inside brackets
within delayed expansion lines starting with !<delimiter>
will be ignored so this can be used for comments:
@echo off
setlocal enableDelayedExpansion
echo delayed expansion activated
!;delayed expansion commented line
echo end of the demonstration
Comment at the end of line
For comments at the end of line you can again use rem
and ::
combined with &
:
echo --- &:: comment (should not be the last line in the script)
echo --- &rem comment
Commenting at the end of file
As noting will be parsed after the exit
command you can use it to put comments at the end of the file:
@echo off
echo commands
exit /b
-------------------
commnts at the end
of the file
------------------
Inline comments
Expansion of not existing variables is replaced with nothing ,and as setting a variable with =
rather hard you can use this for inline comments:
@echo off
echo long command %= this is a comment =% with an inline comment
Multiline comments
For multiline comments GOTO
(for outside brackets) and REM
with conditional execution (for inside brackets) can be used. 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
)
And the same technique beautified with macros:
@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 Sth
Command Or Use This Mark ::: Sth