3

Lately I started to comment lines as double :: , but I'm aware of problems that it can make in long "for" or "choice" scripts and issues with "goto" as it was described What does :: (double colon) mean in DOS batch files?

So I'm wondering if it's ok to set variable as REM and use it?

I've seen somewhere in a little cmd script, and really liked it, since it making batch code looks clearer for me. But I wonder if it wont create any problems?

@echo off
Set #=REM
%#% show date
echo it's %date%
%#% let's wait few seconds...
ping 1.1.1.1 > nul
echo and now it's %date%
%#% whatever
exit
Community
  • 1
  • 1
AdamG
  • 41
  • 2

2 Answers2

3

That is no problem, what-so-ever.

The variable is effectively serving as a macro, and I use that technique frequently (though not for remarks). The batch parser expands the code within the variable prior to any interpretation of the logic of the code, so it functions exactly as you hope/expect.

There is actually a sophisticated technique to create batch macros that take arguments. The post at http://www.dostips.com/forum/viewtopic.php?f=3&t=2518 describes the ultimate syntax, and http://www.dostips.com/forum/viewtopic.php?t=1827 shows the development of the technique over time.

Your use of %#% as a REM macro is perfectly valid, and there is no one "best" style. But I prefer to use :: Comment for remarks that are not within parenthesized blocks, and %= comment =% for remarks that are within parenthesized blocks. That last syntax is simply an undefined variable that gets expanded into nothing. There are only a few dynamic pseudo variables that contain = in the name, and it is impossible for any batch variable to contain two = in the name. So the technique is safe to use as long as the comment does not contain : or %.

dbenham
  • 119,153
  • 25
  • 226
  • 353
1

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.

pieh-ejdsch
  • 196
  • 6