When I write a rem comment in the batch, I put it a blank more than the current code indentation is.
Since a code indentation with me is 2 spaces, it is so for me so readable and falls immediately into the eye.
...
rem my comment
...
Rem comments are helpful when debugging with echo on: The line is read to the end of the line. All %variables%
as well as Parameter %1 ... %9 %*
are used.
This means, however, that all variables within a loop in the rem comment are only resolved at the beginning - because the command rem is displayed once before the execution but remains silent during the execution.
If within parenthesis for debugging all kinds of variables are to be displayed, these must be displayed with echo rem.
For such debugging, it is advisable to set a variable so when debug is off then rem else at debugging echo rem.
If defined debug (set "rem=echo rem") else set "rem=rem"
For other debugging whereby something should not be displayed twice I use the variable %mon%
. This I put on with echo off to echo and with echo on to rem.
rem < :checkEchoOn
:checkEchoOn
:: Liest ob ECHO ON oder OFF geschalten ist um Variablen zu setzen
:: VAR Mon = read ECHO ON / OFF ;set Echo( / rem
rem = :checkEchoOn
@(
rem ^%Mon% Zeilen bei Echo on auf REM umschalten
echo>"%temp%\isOn.Ft"
< "%temp%\isOn.Ft" find "ON" >nul
if not errorlevel 1 set "Mon=rem"
if errorlevel 1 set "Mon=echo("
del "%temp%\isOn.Ft"
rem > END checkEchoOn
if /i "%~0" equ ":CheckEchoOn" exit /b
)
%mon% this is a visible comment - only once
As Dave already writes are the comments starting with 2 doublecolon do not start within loop parenthesis. Make people do it nevertheless in the loops pack.
These :: comments are often an explanation of the batch or function being called.
Please note that these lines are read in completely. %Variable%
and Parameter %1 ... %9 %*
as well as special characters are treated as on a line to be executed (see Jebs declaration).
The alone is already the reason to write these commentary lines carefully.
These :: comment lines can be written as multilines, meaning that the (unmasked) caret at the end of the line, the following line is considered as continuation of the comment. Thus, several successive comment lines without a double point are possible.
The %== loop commentaries %
are used mainly to code overview.
Another kind of comments I use are if-loop commentaries. Which are also used in the debugging but only to the function once to mirror. For this, the comment is either started with *
, or the comment starts just like this.
Note this comments only befor the function begins. The if condition is terminated after the function normally with a closing parenthesis.
...
call :Sub1
if :Sub == begin ( * begin :Sub1
this will begin Sub1
:Sub1
4> "test.txt" (
>&4 echo this is a test
)
exit /b
if :Sub == END * END :Sub1 )
...
This also can be used, for example, in the case of version Alteration.
...
if :new == begin * begin Version 2.1
... new code
if :new == END * END Version 2.1
...
The best way to look at it is how to make it most with the comments and whether it is so usable for you.
The second aspect is that you also understand batches of others as they have written it. As well as that you use the comments to properly document your code for you. It is also for you.