Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Issue with DO when Errorlevel is set to 4095

Jun
170
15
This might qualify as the most bizarre issue I have run across.

I have a command line utility which returns results via an errorlevel value (and stdout, but that's not relevant here). I use that utility in a complex script and at times I saw that the script generated unexpected results which I could not explain. I was finally able to determine how to trigger the behavior and came up with a simple .BTM which doesn't rely on any external utility to demonstrate what I'm seeing.

It appears that when the Errorlevel is set to 4095, and AFAIK no other value, subsequent Do Loops exit after only a single iteration.

Here's the .BTM which demonstrates the issue:

Code:
@echo off
setlocal

          REM ---------  Problem occurs if you select the first 12 items displayed by MULTIPLECHOICEBOX
          REM ---------       - This will return an Errorlevel set to 4095  (twelve 1's in base 2 = 111111111111 = 4095 in base 10)
          REM ---------       - For some reason this value, and only this value, causes subsequent DO loops to exit after 1 iteration
          REM ---------       - So after checking the return code, resetting Errorlevel to 0 makes things work correctly
          REM ---------
          REM ---------       TCC Versions 34-37 Have this issue
          REM ---------       TCC Version 24 Does NOT have this issue

set ItemList=item1;item2;item3

echo.
echo ------------------ First Test ------------------------

REM SETERROR 4095
SETERROR 0

DO 3
  echo.
  echo *** Outer Loop: %_Do_Loop

       set ItemList*
      
       Do Item in /T";" /L %ItemList
          echo Processing %Item
       Enddo
 
enddo


echo.
echo ------------------ Second Test ------------------------

SETERROR 4095

DO 3
  echo.
  echo *** Outer Loop: %_Do_Loop
      
       set ItemList*
      
       Do Item in /T";" /L %ItemList
          echo Processing %Item
       Enddo
  
enddo


quit

And here's the output it generates:

Code:
TCC  37.00.18 x64   Windows 11 [Version 10.0.26200.9974]
Copyright 2026 JP Software Inc.  All Rights Reserved
Your evaluation period expires in 23 days.
You can buy Take Command, TCC, and CMDebug at https://jpsoft.com

[C:\Program Files\JPSoft\TCMD37]c:\batfile\TestEL4095.btm

------------------ First Test ------------------------

*** Outer Loop: 1
ItemList=item1;item2;item3
Processing item1
Processing item2
Processing item3

*** Outer Loop: 2
ItemList=item1;item2;item3
Processing item1
Processing item2
Processing item3

*** Outer Loop: 3
ItemList=item1;item2;item3
Processing item1
Processing item2
Processing item3

------------------ Second Test ------------------------

*** Outer Loop: 1
ItemList=item1;item2;item3
Processing item1

[C:\Program Files\JPSoft\TCMD37]

Note that in the Second Test both Do Loops seem to end after a single iteration.

As noted in the .BTM comments, this is not specific to version 37. TCC versions 34-37 have this behavior. TCC version 24 does not.

Now that I know what is happening, it's a simple enough workaround to reset the Errorlevel after checking it. And I may be the only person in the universe who encountered this, so I don't expect addressing this to be a high priority.
 
The cause is a collision between an ERRORLEVEL value and an internal control-flow sentinel.

TCC uses 0xFFF to indicate "abort the current line". DO does not set or clear ERRORLEVEL, so it does not anticipate that someone will be setting its abort flag before running a DO command.

The workaround is to excplicitly set ERRORLEVEL to 0 before running DO loops.
 
Thanks for confirming the issue and the workaround.

I edited the title of the Post to include a reference to DO in case anyone encounters this obscure situation.
 
Back
Top