"Functions" in cmd.exe batch files

This (test.cmd) works in cmd.exe
Code:
@echo off
ver
where cmd.exe
set "TwoParams(x,y)= x=x,y , y=(x,y)"
set /A "test=%TwoParams(x,y):x,y=123,456%"
echo x=%x%, y=%y%

...returns...
Code:
c:\utils>test.cmd

Microsoft Windows [Version 6.1.7601]
C:\Windows\System32\cmd.exe
x=123, y=456

When I run it under CMDebug using CMD Syntax, it returns;
Code:
TCC-RT  23.00.17 x64   Windows 7 [Version 6.1.7601]
x=123456, y=456

...which is not what I get with cmd.exe

When I run it under CMDebug using TCC Syntax, it returns;
Code:
TCC-RT  23.00.17 x64   Windows 7 [Version 6.1.7601]
C:\Windows\System32\cmd.exe
x=123456, y=456

When I run test.cmd from TCC, it returns;
Code:
c:\users\jlc\utils>test.cmd

TCC  23.00.23 x64   Windows 7 [Version 6.1.7601]
C:\Windows\System32\cmd.exe
TCC: C:\Users\jlc\utils\test.cmd [5]  Unbalanced parentheses "(x"
x=, y=

Joe
 
May 20, 2008
11,380
98
Syracuse, NY, USA
Joe, can you explain what CMD is doing.

At the CMD command line, I get this.
Code:
V:\> set "TwoParams(x,y)= x=x,y , y=(x,y)"

V:\> set /A "test=%TwoParams(x,y):x,y=123,456%"
456
V:\> echo %test%
123

If the "final value of the expression" were 456 (as auto-echoed by SET /A) why was 123 assigned to the variable "test"?
 
A better example;
Code:
@echo off
setlocal EnableDelayedExpansion
ver
where cmd.exe
set "Max(x,y)=( x=x,y, y=(x,y), c=(x-y>>31)+1, c*x+^^^!c*y )"
set "Min(x,y)=!Max(x,y):x-y=y-x!"

echo/
set numbers=123 456
set /A "n1=%numbers: =,n2=%, max=%Max(x,y):x,y=n1,n2%, min=%Min(x,y):x,y=n1,n2%"
echo Max = %max%,  Min = %min%
echo x=%x%, y=%y%

Joe
 

samintz

Scott Mintz
May 20, 2008
1,495
16
Solon, OH, USA
I can sort of grasp the string substitution when invoking a function. But the way the function itself works is voodoo. Can you explain what exactly the MAX function is doing?
Code:
it looks like when the set /A is executed, you get:
set /A "n1=123,n2=456, max=( x=123,456, y=(123,456), c=(123-456>>31)+1, 0*123+^^^!0*456 )"

It looks like SET /A var=x,y sets var to x. And SET /A var=(x,y), sets var to y. And it looks like the expression is evaluated from left to right.
But what do the 3 ^^^'s do?
 

rconn

Administrator
Staff member
May 14, 2008
12,340
149
That syntax is simply bizarre. You're combining two undocumented CMD behaviors to derive a third that I doubt even the CMD authors intended.

I would be very interested in where you found this documented or used, and exactly (as in step by step) what you think it's doing.
 

rconn

Administrator
Staff member
May 14, 2008
12,340
149
The first issue here is that due to the use of invalid characters in the variable name, you have to set CMDVariables=Yes in your TCMD.INI for TCC to accept the variable name in "test=%TwoParams(x,y):x,y=123,456%". TCC then, like CMD, simply accepts anything between the two %'s.

The second issue is that TCC sees "x=123,456" and interprets the comma as a thousands separator, not an argument delimiter. (TCC works fine with the equally silly undocumented CMD syntax "set /a x=1,y=2,z=3" because the comma isn't embedded in a numeric string.)

The third issue is the undocumented CMD behavior when you give it multiple arguments. For example:

set /a test=123,456

echos "456" (at the command prompt, but not in a batch file), and sets test to 123. That's a little odd, but not as odd as issue four:

set /a test=(123,456)

which sets test to 456.

Now, I can support issue #2 by disallowing thousands separators (not a big issue, because only CMD batch files would be using this grotesque syntax anyway).

Regarding #3 and #4 -- I'm unconvinced that the CMD authors intended this behavior (and if they did, someone should slap them upside the head). But if someone wants to post it in the Suggestions Forum (with a convincing explanation of why this is a good idea), I'll consider it for a future version.
 
Similar threads
Thread starter Title Forum Replies Date
A Problem with functions @int @decimal and identifying Powershell as a shell. Support 12
vefatica Library functions and line continuations? Support 4
Charles Dye Documentation Help Nits Forever: Checksum functions Support 0
Joe Caverly SETLOCAL removes Aliases and Functions Support 3
Joe Caverly @NAME supports library functions Support 2
Charles Dye Functions which no-one has ever tried to use.... Support 5
vefatica WHICH and @functions Support 2
vefatica Two @FUNCTIONs/aliases with the same name Support 4
M Keywords for commands, functions, internal variables Support 3
C Documentation near or fully obsolete functions Support 7
Charles Dye Optional F parm in checksum functions Support 0
C coding functions and subroutines Support 9
rps Functions @year[date] @isowyear[date] help Support 7
mikea How to? TCC and XMLnodes/XMLxpath functions Support 0
Dan Glynhampton Are these two functions the same? Support 2
M How to? Logic in functions... Support 5
A WAD function return values with parentheses prevent evaluation of additional functions Support 31
M "History" command not giving me what I want when executed by @Exec... functions... Support 6
Charles Dye Web help on functions Support 2
samintz Help examples for the @ISxxx functions Support 3
R specific functions for number conversion Support 2
C Unsuppressable errors from XML functions Support 5
nikbackm Checksum functions Support 4
J TCC Out of memory with aliases and functions Support 5
Peter Bratton Disappearing functions Support 1
A I dont see anything in the alias/functions/batchvariables tabs in IDE Support 13
dcantor Comments and suggestions on functions for binary buffers Support 4
sully9 How to? Unexpected %DATE% CMD variable format (vs DATE /T format) in CMDebug Support 1
L FTYPE in TCC, less quirky than in cmd.exe Support 3
Peter Murschall Single-line Do-CMD is a bit uncooperative. Support 6
FreezerBurnt Help making a CMD and TCC compatible batch file Support 7
T Date format date different from cmd Support 7
D Differences between TCC and CMD with COPY /b Support 4
M Oracle batch OPatch.bat.bat not working in TCC but correctly in CMD Support 5
E TCC/CMD difference Support 5
S How to? Deinstalling TC/reinstating execution of bat files by cmd in Win7 Support 31
D How to? Pass OpenHere Folder to CMD? Support 6
R Difference between TCC and CMD: variable names with hyphens Support 4
M WAD "Pictures" directory is empty in TCC but not in CMD or Explorer Support 4
rps Cmd history recall Support 4
rps Documentation TITLE cmd docs Support 0
Joe Caverly PUSHD with UNC path in CMD.EXE Support 2
W Starting program for word to pdf conversion (difference cmd and tcc) Support 13
Joe Caverly Using this CMD technique from TCC Support 17
W If command - different with CMD Support 6
I how to make TCC default/replace cmd.exe? Support 9
R FOR usage error from TCC but not from CMD Support 16
I findstr works in cmd but not in tcc Support 5
D Pasting Unicode data has different behavior on TCC and CMD Support 2
M Command line parsing differences between cmd.exe and TCC Support 6

Similar threads