windows batch file with goto command not working

You’ve got two problems.

One problem is that a goto breaks a for-loop.
The other, labels are quite difficult in parenthesis.

The goto breaks always and all nested loops, even if the label of the goto is in the same block, and the for-variables are lost immediately after the jump.

In parenthesis lables are “two line” oriented!
I experimented with labels and here are some results for parenthesis.

When a label occurs, the next line has to be in the correct format for a “secondary” line.

That’s why this fails.

(
:this label fails with a syntax error
)

(
:this works
:because this line is a "legal" secondary line
)

(
:: The remark style
:: fails, because it's not "legal" to use a double colon, because it's not a legal path (in the most cases)
)

(
:and now I got courious & echo This will not echo'd
:but & echo You can see this !
)

For the second line some steps of the batch parser are skipped.

@ doesn’t work, @echo Hello tries to start a file named @echo.bat.

Splitting of parenthesis fails, like in echo( hello.
Labels are handeled as a file name, :echo checks only if :echo is a valid file name and then skip this part.

::hello searches on the drive ::.
For test purposes the drive :: can be created with subst :: c:\temp.
As labels are simply ignored on the second line, ampersands and also pipes work, but the file on :: have to exist.

(
echo @echo This is %~f0
) > %TEMP%\testLabel.bat

REM create Drive ::
subst :: %temp% 
(
:Label 
::\testLabel.bat The bat will not be executed | echo But this
)
subst /D ::

Leave a Comment