How to? Lazy evaluation on %@if[]?

Nov 2, 2009
302
6
Chile
www.farah.cl
I defined a new function, to tell if a given drive is a substituted one or not (following a tip given here a while ago). This function:

function substituted=`%@if["%@left[2,%@truename[%1]]"=="%1",0,1]`

works on ready drives, but fails on non-existing ones (the drives marked with an asterisk are substitutions):

[C:\Users\Miguel\TCC]pwd
NEGRO1 C:\Users\Miguel\TCC\
BLACK2 D:\
NEGRO1 *F:\
NEGRO1 *S:\
NEGRO1 *T:\

[C:\Users\Miguel\TCC]echo %@substituted[C:]
0

[C:\Users\Miguel\TCC]echo %@substituted[T:]
1

[C:\Users\Miguel\TCC]echo %@substituted[X:]
TCC: (Sys) The system cannot find the drive specified.
"X:"



I tried to improve it by prepending a %@ready call, but it didn't work:

[C:\Users\Miguel\TCC]function substituted2=`%@if["%@ready[%1]"=="1" .and. "%@left[2,%@truename[%1]]"=="%1",0,1]`

[C:\Users\Miguel\TCC]echo %@substituted2[C:]
0

[C:\Users\Miguel\TCC]echo %@substituted2[T:]
1

[C:\Users\Miguel\TCC]echo %@substituted2[X:]
TCC: (Sys) The system cannot find the drive specified.
"X:"



I tried to nest to %@if calls instead:

[C:\Users\Miguel\TCC]function substituted3=`%@if["%@ready[%1]"=="1",%@if["%@left[2,%@truename[%1]]"=="%1",0,1],2]`

[C:\Users\Miguel\TCC]echo %@substituted3[C:]
0

[C:\Users\Miguel\TCC]echo %@substituted3[T:]
1

[C:\Users\Miguel\TCC]echo %@substituted3[X:]
TCC: (Sys) The system cannot find the drive specified.
"X:"


... and it doesn't work either.

The help page for @if states "Do not use @IF if evaluating either one of the strings may fail", so it looks I'm out of luck here... or am I?
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,689
106
Albuquerque, NM
prospero.unm.edu
I believe that functions are evaluated "from the inside out", so nesting won't help you here. Perhaps you could create your desired function as a batch file or alias instead, and then define your function using %@EXECSTR[] ...?
 
May 20, 2008
12,175
133
Syracuse, NY, USA
@TRUENAME won't throw an error if you use a backslash. Below, x: does not exist.
Code:
v:\> function issub=`%@if["%@left[3,%@truename[%1\]]"=="%1\",0,1]`

v:\> echo %@issub[c:]
0

v:\> echo %@issub[p:]
1

v:\> echo %@issub[x:]
0
 
May 20, 2008
12,175
133
Syracuse, NY, USA
Actually, that seems to be unfortunate behavior on the part of @TRUENAME ... not caring if a file or directory exists, but caring if a drive exists.
Code:
v:\> echo %@truename[c:\]
C:\

v:\> echo %@truename[c:]
C:\

v:\> echo %@truename[x:\]
x:\

v:\> echo %@truename[x:]
TCC: (Sys) The system cannot find the drive specified.
 "X:"
 
Nov 2, 2009
302
6
Chile
www.farah.cl
Actually, that seems to be unfortunate behavior on the part of @TRUENAME ... not caring if a file or directory exists, but caring if a drive exists.
Code:
v:\> echo %@truename[c:\]
C:\

v:\> echo %@truename[c:]
C:\

v:\> echo %@truename[x:\]
x:\

v:\> echo %@truename[x:]
TCC: (Sys) The system cannot find the drive specified.
"X:"

That worked. Thanks.