Welcome!

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

SignUp Now!

Problem With TEXT ... ENDTEXT

Dec
265
4
I created the following:

@echo off
setlocal
On error, goto ErrorHndl_1

Echo unknown_cmd is set to echo Error in command "%$"

Echo Pre-IFF

IF "%1" == "/h" or "%1" == "/H" or "%1" == "-h" or "%1" == "//?" (
on error goto ErrorHndl_2
echo Pre-Text
TEXT
TEXT command output
ENDTEXT
echo Post-Text
)

Echo Post-IFF
quit
Echo: Why didn't I quit?

:ErrorHndl_1
Echo ErrorHndl_1
quit
Echo: Why didn't I quit?

:ErrorHndl_2
Echo ErrorHndl_2
quit
Echo: Why didn't I quit?

named it test.btm and ran the following:

test /h

and got the following output:

unknown_cmd is set to echo Error in command "%$"
Pre-IFF
Error in command "or "/h" == "/H" or "/h" == "-h" or "/h" == "//?" ( on error goto ErrorHndl_2"
Pre-Text

Echo Post-IFF
quit
echo Why didn't I quit?

:ErrorHndl_1
Echo ErrorHndl_1
quit
echo Why didn't I quit?

:ErrorHndl_2
Echo ErrorHndl_2
quit
echo Why didn't I quit?
ErrorHndl_1


Seems like everything was considered part of the TEXT ... ENDTEXT body, the ENDTEXT was ignored.


So I tried another test and the code is as follows:

@echo off
setlocal
on error goto ErrorHndl_1

Echo unknown_cmd is set to echo Error in command "%$"

Echo Pre-IFF

IFF "%1" == "/h" or "%1" == "/H" or "%1" == "-h" or "%1" == "//?" THEN
on error goto ErrorHndl_2
echo Pre-Text
TEXT
TEXT command output
ENDTEXT
echo Post-Text
ENDIFF

Echo Post-IFF
quit

:ErrorHndl_1
Echo ErrorHndl_1
quit

:ErrorHndl_2
Echo ErrorHndl_2
quit

and got the following output:

unknown_cmd is set to echo Error in command "%$"
Pre-IFF
Error in command "or "/h" == "/H" or "/h" == "-h" or "/h" == "//?" THEN"
Pre-Text
TEXT command output
Post-Text
Error in command "ENDIFF"
Post-IFF

It quit but it seems that the "ENDIFF" command generated an unknown error, I think that this is still related to the TEXT ... ENDTEXT command.

Any thoughts on what I am doing wrong or is this a bug? I am using TCC 36.01.55.

Thanks
 
I did not make any changes to your code.

Code:
@echo off
setlocal
on error goto ErrorHndl_1

Echo unknown_cmd is set to echo Error in command "%$"

Echo Pre-IFF

IFF "%1" == "/h" or "%1" == "/H" or "%1" == "-h" or "%1" == "//?" THEN
on error goto ErrorHndl_2
echo Pre-Text
TEXT
TEXT command output
ENDTEXT
echo Post-Text
ENDIFF

Echo Post-IFF
quit

:ErrorHndl_1
Echo ErrorHndl_1
quit

:ErrorHndl_2
Echo ErrorHndl_2
quit
and got the following output:

unknown_cmd is set to echo Error in command "%$"
Pre-IFF
Error in command "or "/h" == "/H" or "/h" == "-h" or "/h" == "//?" THEN"
Pre-Text
TEXT command output
Post-Text
Error in command "ENDIFF"
Post-IFF

Output
Code:
R:\>test.btm
unknown_cmd is set to echo Error in command ""
Pre-IFF
Pre-Text
TEXT command output
Post-Text
ErrorHndl_2

Joe
 
Hi Joe,

You still got an error. If the "ENDIFF" executed properly, you should have gotten the "Post-IFF" and not "ErrorHndl_2", which is an indicator that the "on error goto ErrorHndl_2" was executed.

Craig
 
I was using an AI to generate a script, and it used "or" instead of ".or.", so the IF statement was the problem, but this error caused the TEXT ... ENDTEXT to not function properly. In my first example, the TEXT command does not seem to be terminated by ENDTEXT.

It appears that TEXT ... ENDTEXT doesn't like being run in Command Groupings.
 
Last edited:
This is interesting. I told Gemini about the TEXT ... ENDTEXT and command grouping, and it gave me the following response:

========

You hit on a very specific, known quirk in Take Command.

You are entirely correct: the TEXT ... ENDTEXT block reads raw characters directly from the script file until it hits the ENDTEXT marker. When you wrap it inside a parenthetical command grouping ( ... ), TCC tries to pre-parse the entire parenthetical block into memory all at once. This breaks the mechanism TEXT uses to read line-by-line, leading to syntax errors or truncated execution.

========

Wow, and I didn't even have to ask Rex for the technical reason it didn't work.
 
As a sidenote,
I prefer to use a Here-Document instead of TEXT/ENDTEXT

Code:
@SETLOCAL
@ECHO OFF
echo Test of TEXT/ENDTEXT
TEXT
This is a test
echo %_isodate
ENDTEXT
echo.
echo Test of TYPE/ENDTEXT
type <<- endtext
This is a test
echo %_isodate
endtext
ENDLOCAL

Output
Code:
Test of TEXT/ENDTEXT
This is a test
echo %_isodate

Test of TYPE/ENDTEXT
This is a test
echo 2026-06-10

You can read more about this in the help file.
Ref: Redirection in TCC
"Here-Document" redirection

Joe
 
This is interesting. I told Gemini about the TEXT ... ENDTEXT and command grouping, and it gave me the following response:

========

You hit on a very specific, known quirk in Take Command.

You are entirely correct: the TEXT ... ENDTEXT block reads raw characters directly from the script file until it hits the ENDTEXT marker. When you wrap it inside a parenthetical command grouping ( ... ), TCC tries to pre-parse the entire parenthetical block into memory all at once. This breaks the mechanism TEXT uses to read line-by-line, leading to syntax errors or truncated execution.

========

Wow, and I didn't even have to ask Rex for the technical reason it didn't work.

And for once the AI gave the right answer the first time!

Yes, TEXT / ENDTEXT is not run through the parser line by line, but as a block until an ENDTEXT is found. TEXT / ENDTEXT is a somewhat archaic solution created 30+ years ago for DOS; there are better ways of doing it now.
 
Back
Top