#1 27 Apr 2020 10:18

npocmaka
Member
From: Bulgaria
Registered: 03 Dec 2009
Posts: 445

https://ss64.com/nt/ - comments page

May be comments page can be updated?

Based on these SO posts:
https://stackoverflow.com/questions/124 … atch-files
https://stackoverflow.com/questions/852 … batch-file
https://stackoverflow.com/questions/418 … acro-files


1. Inline comments:

using a non-existing variable that will be expanded to nothing can be used as comments in batch files (but not in the command prompt directly).As it is hard to create variable which name starts with "=" this technique is commonly used:

@echo off
echo This is an example of inline comment %= Inline Comment =% in the middle of the line.

2. Conditional comments - not a common need ,but can be useful in some situations.

call will not execute a statement closed in brackets and will not change the error level so this can be used as comment in conditional expressions:

echo First Conditional Expression & call ( Conditional Expression Comment ) && echo Second Conditional Expression

I still don't know a command that has no output and changes the errorlevel to be used as a negative conditional comment so only redirection to nul can be used..

Here's an example usage:


@echo off

echo --Case 1:
echo Testing Conditional Execution Logic Flow & call ( comment that enables the positive flow)  && (
	echo positive flow
) || (
	echo negative flow
)

echo --Case 2:
echo Testing Conditional Execution Logic Flow & ?comment that enables negative flow  2>nul && (
	echo positive flow
) || (
	echo negative flow
)

3.Comments at the end of the line

&Rem and &:: can be used - and this is already on the page.

4. Multi line comments:

If the block intended for commenting is outside of a brackets goto can be used:

goto comment
commented
:comment

In brackets a conditional execution can be used:

if 1==1 (
 echo the lines bellow are commented
 rem/||(
    comment where you should be careful with brackets.
 )
)

Goto cannot be used inside brackets because it breaks their context.

The techniques above can be beautified with marcos and here's my favorite way:

@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
)

Though this a matter of personal taste and the last example can be skipped smile


2. Doskey macros in the last link may be will fit better in the DOSKEY page.

Last edited by npocmaka (27 Apr 2020 13:25)

Offline

#2 02 May 2020 11:19

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,083
Website

Re: https://ss64.com/nt/ - comments page

Thanks npocmaka, some great tips in there
I have made some updates to all the following pages
https://ss64.com/nt/rem.html
Multi-Line Comments were already covered, but I have given them a sub-heading so that section is easier to find now.

https://ss64.com/nt/syntax-esc.html
https://ss64.com/nt/goto.html

I put your macro on the macros page, with a link to SO, I think it makes the most sense there:
https://ss64.com/nt/syntax-macros.html

Thanks again, sorry Im a bit slow to update at the moment, but my day job is in the health sector and we are kinda busy at the moment!

Offline

#3 02 May 2020 13:00

npocmaka
Member
From: Bulgaria
Registered: 03 Dec 2009
Posts: 445

Re: https://ss64.com/nt/ - comments page

Simon Sheppard wrote:

Thanks again, sorry Im a bit slow to update at the moment, but my day job is in the health sector and we are kinda busy at the moment!

Oh! I hope you are well and healthy - the health sector really needs a support at the moment.

Offline

#4 17 May 2020 23:24

tomsuters
New Member
Registered: 18 Mar 2020
Posts: 3

Re: https://ss64.com/nt/ - comments page

I like the 'REM/ ||' multi-line comment solution but I can't find anywhere what the '/' after the REM command does to make the conditional || execution work.

Thx in advance.

Offline

#5 18 May 2020 16:07

npocmaka
Member
From: Bulgaria
Registered: 03 Dec 2009
Posts: 445

Re: https://ss64.com/nt/ - comments page

tomsuters wrote:

I like the 'REM/ ||' multi-line comment solution but I can't find anywhere what the '/' after the REM command does to make the conditional || execution work.

Thx in advance.

some internal commands accept some special symbols at the end and are still executed.
Some examples:

echo+
cd.
rem,

and so on. In this case / is doing the same thing as space. I preferred the '/' because it looks closer to the multiline comments in the C-like styled languages , to make it clearer that this is one whole thing, and to look more consistent on different editors.

Offline

Board footer

Powered by FluxBB