Comment Before Commands??

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Comment Before Commands??

#1 Post by Meerkat » 20 Jan 2016 05:30

Hi again guys. Maybe someone else discovered this already.

One time while I am making a Batch game because I am bored, I used a comment like this:

Code: Select all

@echo off
%== Main loop: check for win and lose condition ==%

But when I executed this: This happened...

Code: Select all

'check' is not recognized as an internal or external command,
operable program or batch file.

:shock:
So I tried to experiment:

Code: Select all

%=: @echo off
%=: main 2>nul
%=: for /l %%A in (1,1,10) do (
%=:    for /l %%B in (1,1,10) do (
%=:      if "%%A"=="%%B" (
%This is a good comment :         echo(%%A == %%B
%=:      )
%Hmm... Another comment here:   )
%=: )
%=: pause
%=: goto :main

And the code works as expected. The main part should have 2>nul because this is treated as both a command and a label.
I do not know why is this happening, but I expect that you know this already.

Also I expect that this does not have any applications. I tried hybridization, but % is rarely used as a comment. :roll:

Meerkat

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Comment Before Commands??

#2 Post by jeb » 20 Jan 2016 06:20

Hi Meerkat,

the cause of your problem is the colon, it starts a special handler for the variable, but when the variable (in your case "== Main loop") isn't defined, then the text after the colon will be used.
Dave's compendium about variable expansion is at SO: How does the Windows Command Interpreter (CMD.EXE) parse scripts?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Comment Before Commands??

#3 Post by Squashman » 20 Jan 2016 07:26

I believe this was also discussed here in one of the Batch Macro threads.
viewtopic.php?f=3&t=5374

Post Reply