Display the program that will run when type a name at the cmd prompt

You can also find this file here: http://www.the-wildwest.com/Queeg/Batches/SearchPath.bat

:: ************************************************************************* ::  SearchPath.Bat                                               10/31/2005 ::  Written by Brian Layman (AKA Capt. Queeg) ::  Visit him at http://www.thecodecave.com/ ::  ::  A batch written to display the program that would be run when ::  a filename is typed at the command prompt.  Just a demo for ::  Hartmut at http://www.ztw3.com/forum/forum.cgi ::  ::  Usage: SearchPath ProgramName[.EXT] ::  ::  History: ::     10/31/2005 - BL - Created ::     11/01/2005 - BL - Removed Temp File Usage ::     02/28/2006 - BL - Changed my URL ::  :: ************************************************************************* @echo Off :: All this is boiled down to one subroutine that sets a variable of the :: same name. call :SearchedFilePath %1 :: If no program is found, say so. if "%SearchedFilePath%"=="" echo There is no matching program in the search path :: If a program was found, echo its name. if NOT "%SearchedFilePath%"=="" echo %SearchedFilePath% :: Clear out our temp variable set SearchedFilePath= :: Quit GOTO :EOF :: ************************************************************************* :: ************************************************************************* ::  Support procedures :: ::  These routines are called with a CALL directive and the GOTO :EOF ::  terminates that CALL but does not terminate the entire running of the ::  batch file. :: ************************************************************************* :: ************************************************************************* :SearchedFilePath ::  Returns the full path to a passed file in the searchpath :: ::  Returns blank if not found. :: :: ************************************************************************* : set SearchedFilePath=    :: Set the default value to blank.   set SearchedFilePath=   :: If there is no extension handle it   if "%~x1"=="" Call :SearchWithExtensions %1&GOTO :EOF   :: There is no extension, is it blank?   if "%1"=="" GOTO :EOF   :: So, we have an extension.  That means we can do a simple search.   :: %~dp$PATH:1 automatically searches the path for us.  It is a   :: variable set by the Call command.   set  SearchedFilePath=%~dp$PATH:1%1   if "%SearchedFilePath%"=="%1" set SearchedFilePath=&GOTO :EOF   GOTO :EOF :: ************************************************************************* :: ************************************************************************* :SearchWithExtensions ::  Iterates the extensions gathered from the PATHEXT environment ::  and searches until the file is found. :: ::  Returns blank if not found. :: :: *************************************************************************     :: Initialize a counter for looking at multiple search results in one line     set cnt=0     :SearchLoop       :: Break out after 20  checks.       :: If you might have more than 20 extensions, increase this value.       :: If you could find out how many periods there are in the temp file, you could optimize this.       if "%cnt%"=="20" GOTO :SearchLoopCleanup       set /A cnt=%cnt%+1       :: Continually search the single line file returning each sequential search result and recursively pass it to the       :: SearchedFilePath routine.  When we ask for a token # that doesn't exist and blank is returned, abort out.       for /F "tokens=%cnt% delims=.;" %%C in ("%PATHEXT%") do call :SearchedFilePath %1.%%C       if "%SearchedFilePath%"== "" GOTO :SearchLoop         :SearchLoopCleanup       :: Clear our Temp variable       set cnt=   GOTO :EOF :: *************************************************************************

 

Add a Comment

Your email address will not be published. Required fields are marked *