How to? Logic in aliases...

May 24, 2010
Northlake, Il
As was pointed out to me in another thread program logic can be placed into aliases. I didn't assume/know this because I really didn't think of logic "commands" as "commands" in the usual sense; i.e. in TCC "IF", "ELSE", "DO" and "ENDO" for instance are not just logic elements (as they are in most other languages), they are actually commands. So testing this out I came up with the following alias:
Alias Logic=`SetLocal & Set C=0 & Set I=1 & Do While %I LT 65536 & Set /A I+=I & Set /A C+=1 & EndDo & Echo C: %C I: %I & EndLocal`
Unfortunately, executing this alias has the following result:
Usage : DO [n | FOREVER]
TCC: Unknown command "EndDo"
C: 1  I: 2
So turning the body of the above alias into a batch file:
[Z:\]Type LogicI.btm
SetLocal & Set C=0 & Set I=1 & Do While %I LT 65536 & Set /A I+=I & Set /A C+=1 &
EndDo & Echo C: %C  I: %I & EndLocal
Set C=0
Set I=1
Do While %I LT 65536
Cancel batch job Z:\LogicI.btm ? (Y/N/A) : A
a different error (apparently an infinite loop) but still an error.

However putting every statement on a single line:
[Z:\]Type LogicII.btm
@Echo Off
Set C=0
Set I=1
Do While %I LT 65536
  Set /A I+=I
  Set /A C+=1
Echo C: %C  I: %I
C: 16  I: 65536
The only "changes" were the addition of the "@Echo Off" commandand separation of the individual commands by linefeeds rather than command separator characters.

And even something like "Do 16" is not valid because "EndDo" is not valid in an alias:
[Z:\]Type LogicIII.btm
@Echo Off & SetLocal & Set C=0 & Set I=1 & Do 16 & Set /A I+=I & Set /A C+=1 & End
Do & Echo C: %C  I: %I & EndLocal
TCC: Z:\LogicIII.btm [1]  Unknown command "EndDo"
C: 1  I: 2
So does this mean the only logic "command" allowed in an alias is the "If"?
May 20, 2008
Elkridge, MD, USA
Your problem is that you ignored the CAVEAT on the help page for the DO command - DO and ENDDO must be on separate lines. It is possible to use the "single line DO" in an alias, but it executes a command group, i.e., the command(s) must be enclosed in parentheses (command).
May 24, 2010
Northlake, Il
Thank you, Steve. But my problem isn't that I "ignored" the CAVEAT, it's that I didn't see it because I'm half blind and therefore tend to automatically (as in subconsciously) read the minimum amount of stuff that I think I need to read; and this especially a problem for old material I have looked at multiple times in the past but didn't remember all of the details (I have also been diagnosed as having very bad memory). But the bottom line is that is good to know.


Scott Mintz
May 20, 2008
Solon, OH, USA
Alias Logic=`SetLocal & Set C=0 & Do I=1 to 65536 (Set C=%@inc[c]) & Echo C: %C I: %I & EndLocal`
C: 65536 I: 65537
Similar threads
Thread starter Title Forum Replies Date
M How to? Logic in functions... Support 5
M How to? Put logic into an alias... Support 6
Jay Sage Parameter %$ Different in Aliases and BTMa Support 4
vefatica Questions about aliases. Support 1
Jay Sage Request Re Aliases CD_LEAVE and CD_ENTER Support 2
Joe Caverly Expand all aliases on the command line Support 2
S Take Command crashes for aliases with length > 1015 Support 1
Joe Caverly SETLOCAL removes Aliases and Functions Support 3
vefatica Two @FUNCTIONs/aliases with the same name Support 4
D Can ANSI escape sequences be used in keystroke aliases? Support 8
Joe Caverly Documentation Aliases Overview Support 3
vefatica Display of special characters in aliases. Support 25
T aliases disappearing Support 22
J Autoexecute Keystroke Aliases Support 1
vefatica Aliases ... appending unused parameters? Support 2
cgunhouse Problem with CALL and Directory Aliases in V17 Support 9
MickeyF WAD 'if' and aliases don't mix so well Support 5
J Saving aliases Support 21
vefatica Backquotes, aliases get 'em, BTMs don't? Support 1
vefatica Aliases global vs. local Support 12
R key aliases no longer working Support 2
newbie Test to ensure aliases are loaded Support 6
D Are local aliases inherited? Support 4
T FTP.CFG file aliases Support 5
vefatica @@key aliases cause new prompt? Support 16
J TCC Out of memory with aliases and functions Support 5
J Problem with aliases Support 6
P Quick help for aliases Support 3
R Alt-key aliases? Support 3

Similar threads