1

One day, I'm bored, I opened up cmd and typed in this piece of code:
@echo off & color 0a & set /a count=0 & main & set /a count+=1 & echo %count% & goto main
And surprisingly, the program executed for only a second. 0_o

I then wrote the code in the normal batch script format, which is like:

@echo off color 0a set /a count=0 :main set /a count+=1 echo %count% goto :main 

Obviously no errors, program runs perfectly.

So what's going on with the 1st piece of code? The command is the same (different format though) but why is it executed for only a sec?

Thanks in advance!

2
  • Might be wrong, but I think you need && instead of &. Commented Aug 1, 2015 at 9:08
  • 2
    no, & is fine and correct. This is a delayed expansion problem (and of course, goto doesn't work on commandline - even if the label was correct (:main)) Commented Aug 1, 2015 at 9:16

1 Answer 1

2

Your main doesn't have a colon which a label requires, and you can't have a label inside a compound statement using a goto - which is your essential problem.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.