1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

@WHICH[]

Discussion in 'T&T - Functions' started by millardjk, Feb 16, 2011.

  1. millardjk

    Joined:
    Aug 22, 2008
    Messages:
    29
    Likes Received:
    0
    Ever need to know if a given command is available within your batch file at runtime? This function lets you know if a command is available and what it's source is by using the internal WHICH command as a function:

    Code:
    which   =%@exec[@set _which=%@left[3,%@word[3,%@execstr[which %1]]]]%@if[%_which eq int,1,%@if[%_which eq ali,2,%@if[%_which eq ext,3,%@if[%_which eq bat,4,%@if[%_which eq exe,5,%@if[%_which eq wit,6,%@if[%_which eq unk,-1,0]]]]]]]%+unset _which
    
    The return value lets you know the type:
    1: internal command
    2: alias
    3: external
    4: batch
    5: Executable extenstion
    6: Windows file association
    -1: unknown

    {yes, it's a crazy, long function and I wish Rex would incorporate it directly into the API because I use it all the time}
     
  2. Peter Bratton

    Joined:
    Jul 1, 2008
    Messages:
    81
    Likes Received:
    0
    Here's a shorter version:
    Code:
    %@dec[%@eval[%@index[unk xxx int ali ext bat exe wit,%@left[3,%@word[3,%@execstr[which %1]]]] / 4]]
     
  3. epement

    Joined:
    Jun 28, 2008
    Messages:
    67
    Likes Received:
    2
    If you don't care where the command is located (because you already know by looking at the name), this works for me, assuming that the command name is %1:

    (which %1 | ffind /E"external|alias|batch" /B >NUL) || (echo ERROR! %1 is not available! Exiting ... %+ quit)
     

Share This Page