:: WHICH.CMD :: also see http://www.robvanderwoude.com/which.html @echo off rem rem T Fitzpatrick 1999 rem rem A script to find a program on the PATH rem rem see :usage for help message rem if "%1"=="" goto usage if "%2"=="" goto arg2OK if "%2"=="e" goto arg2OK goto :usage :arg2OK set EXTENSION=%~x1 set foundFileName= if NOT "%EXTENSION%"=="" goto EXTENSION_DEFINED rem --- Search for .bat and .exe files if no extension given --- call :s_which1 %1.bat if "%foundFileName%"=="" call :s_which1 %1.cmd if "%foundFileName%"=="" call :s_which1 %1.exe rem --- Fall through to search for extension-less file --- rem --- Only search for exactly named file if an extension was defined --- :EXTENSION_DEFINED if "%EXTENSION%"==".lib" echo. && echo Search using LIB variable... && echo. if "%EXTENSION%"==".h" echo. && echo Search using INCLUDE variable... && echo. if "%foundFileName%"=="" call :s_which1 %1 if not "%2"=="e" goto noEditorOpen if not "%foundFileName%"=="" start notepad %foundFileName% :noEditorOpen if not "%foundFileName%"=="" dir %foundFileName% | findstr -i %1 goto :EOF :: ---------------------------------- :: Subroutine s_which1 :: ---------------------------------- :s_which1 if "%EXTENSION%"==".lib" set foundPath=%~dp$LIB:1&& goto doneSearch if "%EXTENSION%"==".h" set foundPath=%~dp$INCLUDE:1&& goto doneSearch set foundPath=%~dp$PATH:1 :doneSearch if not "%foundPath%" == "" set foundFileName=%foundPath%%1 if "%foundPath%" == "" set foundPath=File not found: echo %foundPath%%1 goto :EOF :: ---------------------------------- :usage echo. echo which.bat is a script to find a program on the PATH echo T Fitzpatrick 1999 echo. echo Usage : which ^ [e] echo If ^ has no extension then which.bat will search echo for .bat, .cmd and .exe files starting with ^, echo otherwise it searches for ^ only echo When the extension is specified and is ".lib" then the LIB echo environment variable is used in the search echo When the extension is specified and is ".h" then the INCLUDE echo environment variable is used in the search echo Optional e arg indicates open the found file with notepad echo.