create an alias with optional parameter

Jun 3, 2008
127
3
Temecula, CA
I'm trying to create a simple alias that calls a program with the parameter(s) passed to it, after massaging them slightly. The first parameter is required, and the second is optional. If the second is not supplied, I want to take the first parameter, use just its filename, and prepend the _cwd. Both parameters must be properly quoted, if needed, and both must be full paths, not relative.

Here is what I created, but it doesn't work if the second isn't supplied, complaining about not being able to find %2 that won't exist. In other words, it's not doing short-circuit evaluation, because it shouldn't need to do the second part of the @if because the conditional evaluates true. How do I change this to work properly?

RichCopy="c:\Program Files (x86)\Microsoft Rich Tools\RichCopy 4.0\RichCopy.exe" %@quote[%@full[%1]] %@if[%# == 1,%@quote[%_cwd\%@filename[%1]],RichCopy %1 %@full[%2]]
 
Last edited:

samintz

Scott Mintz
May 20, 2008
1,503
16
Solon, OH, USA
Aliases don't do short circuit evaluation. They evaluate the entire command line at once. Press Ctrl-F after typing your command line (before pressing ENTER) and you will see what is being executed.

Create a function that does the substitution for you.
Code:
function foo=%%@if[%%2.==.,%%1,%%2]
echo %@foo[1,2] %@foo[1]
2 1

Then your alias becomes:
Code:
alias RC=RichCopy "%%1" "%%@foo[%%1,%%2]"
 
Jun 3, 2008
127
3
Temecula, CA
Thanks, Scott! I didn't know that aliases didn't perform short circuit evaluation.

Here is my alias and companion function that works in all of the test cases I tried (as copied from my .als and .fns files):

RichCopy="c:\Program Files (x86)\Microsoft Rich Tools\RichCopy 4.0\RichCopy.exe" %@quote[%@full[%1]] %@quote[%@if[%# == 1,%@quote[%_cwd\%@filename[%1]],%@full[%@Maybe2nd[%1,%2]]]]

Maybe2nd=%@if[%2.==.,%1,%2]
 

samintz

Scott Mintz
May 20, 2008
1,503
16
Solon, OH, USA
I think you're over complicating it.
Code:
function qfull=%@quote[%@full[%1]]
RichCopy="c:\Program Files (x86)\Microsoft Rich Tools\RichCopy 4.0\RichCopy.exe" %@qfull[%1] %@qfull[%@Maybe2nd[%1 %2]]
 

samintz

Scott Mintz
May 20, 2008
1,503
16
Solon, OH, USA
As far as I know, quoting all your file names doesn't hurt anything. So you could shorten it by removing @quote and just use quotes.
Code:
RichCopy="c:\Program Files (x86)\Microsoft Rich Tools\RichCopy 4.0\RichCopy.exe" "%@full[%1]" "%@full[%@Maybe2nd[%1 %2]]"

BTW - You will run into an issue if the file names you are using have spaces in them.
Code:
echo %@Maybe2nd[hello world.txt, foo bar.txt]
world.txt
 
Similar threads
Thread starter Title Forum Replies Date
Joe Caverly Create Windows Jobs and optionally attach processes to a job Support 17
E How to? Create a portable version of TCMD to use on other computers w/o installation Support 4
Alpengreis Fixed 0xc0000005 while try to create a button (Schaltfläche) Support 4
G How to? Trying to create TCC shortcut that opens with blue background (on Windows 8.1 x64) Support 1
noahcoad How to? Create shortcut with env var in path Support 4
S Problems to create new threads Support 6
C How to? create a JPSoft BTM menu to run custom custom batch files Support 2
A How to? Create a date range based upon creation date Support 2
S How to? Change thread prefix? Easy "create thread" method? Support 8
Stefano Piccardi Why did Windows create an encryption key? Support 5
S Create links or junctions on network drive Support 5
Jay Sage How Can SENDMAIL Create Messages with Plain Text and HTML Support 2
G create standalone .exe? Support 1
Charles Dye Create empty registry value? Support 5
D Can alias pass parameters to for command? Support 3
Jay Sage Issue with CD_ENTER Alias Support 37
Jay Sage Issue with CD_LEAVE Alias Support 3
Jay Sage WAD Failure of Alias Command with /Z Option Support 14
R @select inside alias gives different (insanely small) window Support 7
kb6ojs ALIAS with multiple commands? Support 9
vefatica Can I alias @@Ctrl-Shift-Alt-u? Support 4
D Alias with setdos no longer works Support 21
vefatica TPIPE in an alias? Support 2
Peter Murschall TCC V26.01.3x Debugging has problems with Alias /R Support 1
Peter Murschall CMDebug 26.01.37 has problems with Alias Support 21
samintz How to? Alias or prompt? Support 1
MickeyF expand alias in cmdline with Ctrl-F in TCC Support 21
Jay Sage Directory Alias Access to FTP Servers Support 2
rps How to? Shralias and producing history.sav, alias.sav .. Support 6
J Alias @@F5 hotkey Support 2
vefatica alias.sav not accurate Support 8
Joe Caverly Using a Directory Alias with @iniwrite fails Support 14
N multiple command alias and redirection Support 0
vefatica SETLOCAL ... ALIAS ... pipes Support 2
B TCC 21.01.50 Problem with ALIAS /r and SET /r Support 2
rps How to? Alias/Command line use of *log on/off Support 2
MickeyF alias terminating before it should Support 3
JohnQSmith ALIAS /R in TCSTART.BTM Support 9
D Alias in batch file Support 3
vefatica Can't execute @ALIAS[...] Support 5
E How to? Load Alias File on Startup Support 3
I Windows 10 console does not allow alt-F4 alias to complete Support 1
nickles Alias expansion Support 7
T scrput `` in alias or function Support 2
vefatica Alias works differenltly v16 vs. v17 Support 13
B Alias with multi line definitions not assigned Support 6
jbanaszczyk Alias with space in the name Support 3
jbanaszczyk Alias for internal command Support 8
D Clear alias parameters Support 7
T Fixed multi-line alias issue Support 13

Similar threads