Batch setting REM as a variable to make the code look different

Lately I started commenting lines as double ::, but I am aware of the problems that can arise in long "for" or "select" scripts and problems with "goto" as described What does :: (double colon) mean in batch DOS files?

So I am wondering if it is possible to set a variable as REM and use it?

I saw a little cmd script somewhere and really liked it as it makes the batch code more understandable for me. But I'm wondering if any problems will be created?

@echo off
Set #=REM
%#% show date
echo it %date%
%#% let wait few seconds...
ping 1.1.1.1 > nul
echo and now it %date%
%#% whatever
exit

      

+3


source to share


2 answers


It's not a problem, something is always.

A variable effectively serves as a macro, and I use this technique a lot (though not for comments). The batch parser expands the code inside the variable to any interpretation of the code's logic, so it functions exactly as you hope / expect.



There is actually a complex technique for creating batch macros that take arguments. A post at http://www.dostips.com/forum/viewtopic.php?f=3&t=2518 describes the final syntax and http://www.dostips.com/forum/viewtopic.php?t=1827 shows the development of the technique with the passage of time.

Your use of a macro %#%

as a macro is REM

perfectly fair, and there is no "best" style. But I prefer to use it :: Comment

for notes that are not in parentheses and %= comment =%

for notes that are in square brackets. This last syntax is just an undefined variable that expands to zero. There are only a few dynamic pseudo variables that are contained =

in the name, and it is not possible for any batch variable to contain two =

in the name. Thus, this method is safe to use if the comment does not contain :

or %

.

+3


source


When I write a rem comment in a package, I leave it blank more than the current code indentation. Since the code indentation with me is 2 spaces, it is so readable to me and immediately striking.

...
 rem my comment
...

      

Rem notes are useful for echoing debugging: the line is read to the end of the line. All %variables%

are used as well as the parameter %1 ... %9 %*

. This means, however, that all variables in the loop in the rem comment are only allowed at the beginning - since the rem command is displayed once before being executed, but remains silent during execution.

If all sorts of variables are to be displayed in brackets for debugging, they should be echoed back. For such debugging it is recommended to set a variable, so when debugging is disabled then rem else when debugging echo rem.

If defined debug (set "rem=echo rem") else set "rem=rem"

      

For other debugging where something shouldn't show up twice, I use a variable %mon%

. It was I who turned on the echo for echo and echo reply.

 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 wrote, comments starting with 2 doublecolon do not start in a loop parenthesis. Get people to do it, though, complete with hinges. These :: comments are often the explanation of the batch or function being called.



Note that these lines are fully readable. %Variable%

both the parameter %1 ... %9 %*

and special characters are treated as an executable string (see the Jebs declaration). One of them is already a reason for writing these comments. These :: comment lines can be written as multi-line, which means that the (non-greased) caret at the end of the line, the next line is considered a continuation of the comment. Thus, multiple consecutive lines of comments without a double dot are possible.

%== loop commentaries %

are mainly used for code review.

Another kind of comments that I use are if-loop comments. Which are also used in debugging, but only for the function once for mirroring. To do this, the comment begins either with *

, or the comment begins that way. Note these comments just for the start of the function. The if condition is terminated after the function with the 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 can also be used, for example, in the case of a version modification.

...
if :new == begin * begin Version 2.1
... new code
if :new == END * END Version 2.1
...

      

The best way to look at this is how to do it with comments and how comfortable it is for you. The second aspect is that you also understand the games of others as they wrote them. Also, you use comments to properly document your code for you. This is also for you.

+1


source







All Articles