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
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 %.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With