[14.02.36] leavefor in @execstr issue

May 31, 2008
382
2
The following batch file never goes past the @execstr line.
Code:
setlocal
set cmd=TODO. "a b"
set x=%@execstr[for %%i in (%[cmd]) (echo %%i ^&leavefor)]
echo x=%x
endlocal
quit
output
Code:
setlocal
set cmd=TODO. "a b"
set x=TODO.
Changing the code to using gosub reveals that execution does continue past @execst but there seems to be one extra pop of the return stack, I guess.
Code:
setlocal
gosub test
echo here!
endlocal
quit
:test
set cmd=TODO. "a b"
set x=%@execstr[for %%i in (%[cmd]) (echo %%i ^&leavefor)]
echo x=%x
return
output
Code:
setlocal
gosub test
set cmd=TODO. "a b"
set x=TODO.
echo here!
here!
endlocal
quit
TCC 14.02.36 Windows XP [Version 5.1.2600]
TCC Build 36 Windows XP Build 2600 Service Pack 3
 
May 20, 2008
11,520
102
Syracuse, NY, USA
It works as expected if you don't escape the '&' (why did you escape it?). When it's escaped, I'd expect it to be echoed but it's not and execution doesn't continue.
Code:
v:\> type forleave.btm
setlocal
set str=a b c
set z=%@execstr[for %%x in ( %[str] ) (echo x = %%x & leavefor)]
echo %z
echo foo
echo bar
 
v:\> forleave.btm
x = a
foo
bar
 
v:\> type forleave.btm
setlocal
set str=a b c
set z=%@execstr[for %%x in ( %[str] ) (echo x = %%x ^& leavefor)]
echo %z
echo foo
echo bar
 
v:\> forleave.btm
 
v:\>
 
May 31, 2008
382
2
Doesn't & inside @execstr need to be escaped in general? I'm confused; what do you think is happening in the second example below?
Code:
[C:\]echo %@execstr[echo a & echo b]
a

[C:\]echo %@execstr[echo a ^& echo b]
a
 b
 
May 20, 2008
11,520
102
Syracuse, NY, USA
Doesn't & inside @execstr need to be escaped in general? I'm confused; what do you think is happening in the second example below?
Code:
[C:\]echo %@execstr[echo a & echo b]
a
 
[C:\]echo %@execstr[echo a ^& echo b]
a
b
I think the parens are protecting it ... perhaps too well ... it can't be escaped to be made a literal '&'.
Code:
v:\> echo %@execstr[for %%x in ( a b c ) (echo x = %%x & leavefor)]
x = a
 
v:\> echo %@execstr[for %%x in ( a b c ) (echo x = %%x ^& leavefor)]
x = a
 
v:\> echo %@execstr[for %%x in ( a b c ) (echo x = %%x ^^& leavefor)]
x = a ^
 
May 20, 2008
11,520
102
Syracuse, NY, USA
I didn't try hard enough.
Code:
v:\> echo %@execstr[for %%x in ( a b c ) (echo x = %%x ^^^& leavefor)]
x = a & leavefor
 
v:\>

But that doesn't work in a batch file. Regardless of how many times the '&' is escaped, you get the screwy behavior.
 
May 20, 2008
3,515
4
Elkridge, MD, USA
This is the penultimate paragraph of the documentation:
@EXECSTR involves several extensive internal processing stages. You might be able to use more complex command sequences (pipes, command groups, etc.) as its parameter, but always test carefully first as the results may not always be what you expect. We recommend that you only use a single command (internal, external, batch file, etc.) parameter.
IMHO there is no problem.

Here are some points about the reason for the observed behavior.
- escaped command separator (e.s.c.)
in first pass it is not interpreted as a command separator, thus the characters behind it are considered part of the command preceding the e.s.c. Once the WHOLE command containing the @EXECSTR is executed, it is found by the parser, and executed as a command outside of the @EXECSTR context. This behavior may be considered "screwy", but well within the warnings quoted above.
- multiple ECHO commands separated by command separator
the first line written to STDOUT is returned as the value of @EXECSTR; all subsequent lines discarded
 

rconn

Administrator
Staff member
May 14, 2008
12,404
152
The following batch file never goes past the @execstr line.

I don't understand what you're trying to do, but your syntax almost certainly won't result in what you're expecting.

Don't escape &'s (or |'s) inside command groups. What's happening is that the "& LEAVEFOR" is getting put into the output of @EXECSTR, so your SET statement expands to

set x=TODO. & leavefor

and the LEAVEFOR with no matching FOR is causing your batch file to close. (This is WAD.)
 
May 20, 2008
11,520
102
Syracuse, NY, USA
I didn't go far enough earlier (again). Though I doubt it was Stefano's intention, you can get the '&' echoed if you escape it enough. And (below) I don't see "x = b & leavefor" because that's the second line of output.

Code:
v:\> type forleave.btm
echos 1^t
echo %@execstr[(echo x = x ^^^& leavefor)]
set z=%@execstr[for %%x in ( a b ) (echo x = %%x ^^^^^^^& leavefor)]
echos 2^t
set z
echos 3^t
echo %z

v:\> forleave.btm
1       x = x & leavefor
2       x = a ^& leavefor
3       x = a & leavefor
 
May 20, 2008
11,520
102
Syracuse, NY, USA
I have no idea what you're trying to do here ...
Earlier I had tried to get the "& leavefor" to be echoed (and failed). I was just pointing out that it can be done while (perhaps) pointing out the nuances of escaping ... just educational (for me too).
 
May 31, 2008
382
2
I don't understand what you're trying to do...
Separating the first, possibly quoted multiword, argument from the tail of %cmd
Code:
E:\>set cmd="a b c" x y z
 
E:\>echo %@execstr[for %%i in (%[cmd]) (echo %%i &leavefor)]
"a b c"
 
E:\>set cmd=a "x y z"
 
E:\>echo %@execstr[for %%i in (%[cmd]) (echo %%i &leavefor)]
a
 
Similar threads
Thread starter Title Forum Replies Date
vefatica @EXECSTR ... TCSTART ... something's wrong! Support 7
M Server 2019 Bug with @execstr? Support 14
vefatica @EXECSTR clobbers @ECHO ON Support 2
T @execstr unicode support Support 6
x13 %@EXECSTR[-X,...] generates CLS before execution? Support 10
Joe Caverly Documentation Add see also _EXECSTR to @EXECSTR Support 0
T @execstr[] on Win10 Support 5
M @ExecStr vs. Call Support 4
M A problem with nested Gosub's/ExecStr's Support 14
J goto compat issue makes all node/npm wrapper scripts fail to run... Support 2
cgunhouse TCToolBar /W Issue Support 2
G v28 Display Issue Support 7
Jay Sage Issue with CD_ENTER Alias Support 37
Jay Sage Issue with CD_LEAVE Alias Support 3
fpefpe How to? issue with % and evaluation Support 5
cgunhouse Standard User Account Issue Support 2
samintz WAD Display wrapping issue Support 5
M TCC color issue in ConEmu Support 4
samintz Timer issue Support 4
Alpengreis Documentation Minor issue in help file for view /E Support 1
samintz WAD ANSI issue Support 3
D TCSTART issue after reinstallation Support 14
A Documentation [Help file] OPTION "//" synopsis formatting issue Support 0
Chen Touboul Overtyping issue - i press the Insert key, help till press enter for new line in TCC Support 2
Y TCC and Cmder imcompatibility issue Support 7
M Another possibly strange remote registry issue Support 5
T WAD bpokestr issue Support 5
D High-DPI Scaling issue Support 1
S CMDebug 22 issue with del command Support 1
cgunhouse Foldermonitor Issue Support 7
samintz Build 38 Install issue Support 5
rps WAD Setlocal issue Support 4
Luiz Rodrigues Issue with Maven Support 1
Seven Update / install issue with 20.0.20.10.32 take command Support 7
Fross WebDav Drive Mapping Issue Support 19
D Upgrade issue Support 3
Fross V20b8 Everything Issue Support 5
T TCC display issue Support 1
cgunhouse _logfile Issue Support 5
T Fixed Querybox issue Support 1
cgunhouse Fixed Share Memory Issue Support 5
cgunhouse Foldermonitor with "/E" Option Issue Support 6
Fross TabComplete Argument Issue Support 1
fpefpe How to? Stange start up issue Support 1
C cosmetic issue with LIST's (I) Support 9
jbarnes1967 TC 18.00 x64 issue with lua io.popen() Support 2
D How to? Wildcards as sets - looping issue with FOR and REN Support 7
T WAD Free issue Support 6
R Fixed v17.0.54 Tab->COMSPEC Dialog Box Issue Support 2
R Minor Cosmetic Issue in Take Command Window Support 2

Similar threads