Many thanks for your report!
Because of it I uncovered another secret of Microsoft's cmd
The solution to your problem is very simple
Please change your code file's extension to .cmd
or add a line cd. before set /a in the code, to reset the error code to 0
==================================
Now let me describe the process of solving the problem
First, after downloading your test file
I tested it and indeed found the problem you described
Then I compared the problem code with my source code that runs normally
and found that the problem code had two extra spaces at the ends of line 2 and the line containing the first goto Main
Because of the special position of the 2nd space
I thought I had found the cause of the problem
Because I use EditPlus to edit source code
it automatically clears trailing spaces when saving
and the code posted to the forum might have gained them after being modified with some other editor
However, after removing the extra spaces and testing, I found the problem still existed
A binary comparison of the two code files again showed they were completely identical
Then I began a long tracing and debugging process
I found that once the problem code generated errorlevel 9167 because of an error in set /a
it would no longer reset errorlevel to 0 because of a correct set /a later
while the normal code did not have this problem
After thinking it over again and again
I finally determined that this problem is related to the file extension
That is,
in a .bat file, an error generated by set will never be reset by set itself
while in a .cmd file, it can be
Here is a piece of test code
Save it separately as .bat and .cmd files and test it
you will find the results differ
At this point the issue is basically clear
@echo off
set /a ii=23 + 12
echo %errorlevel%.
set /a ii=23 + 09
echo %errorlevel%.
echo %errorlevel%.
set ii=23
echo %errorlevel%.
pause
But as for the deeper reason for this problem, I still haven't found it
I'll leave that for interested people to continue exploring
Also, when I ran the .bat file from the EditPlus toolbar, I did not encounter this problem