Copy the below code and save it as whereis.bat file and put it into windir
@for %%e in (%PATHEXT%) do @for %%i in (%1%%e) do @if NOT "%%~$PATH:i"=="" echo %%~$PATH:i
This will almost work same as the whereis command in linux.
The WHEREIS.BAT script does not search the current directory, nor does it allow you to explicitly specify a command extension.
It can erroneously list a folder instead of a file.
It is relatively slow.
The WHERE command lists all possible matching files. It properly searches the current directory first and also handles explicit file extensions.
But it would be nice if it had an option to list the file that would execute only. That is easily done by using WHERE in a batch FOR loop and exiting as soon as a file is found. But WHERE is relatively slow, so there is a better solution.
The WHICH.BAT script is very nice in that it properly handles the current directory as well as explicit file extensions. It is also nice that it detects internal commands.
But it can erroneously report a folder instead of a file.
It is also relatively slow.
Below is my version of WHICH.BAT that combines the best features of all of the above, and is very fast and I believe bullet proof.
Some additional improvements:
- Provides an option to store the result in a variable instead of echoing to the screen.
- Sets the ERRORLEVEL to 1 if the file is not found but the command is an internal command. It also outputs an appropriate error message on stderr.
- Sets the ERRORLEVEL to 2 if the file is not found and it is not an internal command. Also outputs an appropriate error message on stderr.
I was forced to do a bunch of delayed expansion toggling because my algorithm needs to expand PATH. But %PATH% will fail if it contains a folder name with an unquoted special character like &. So I need to use !PATH!. But FOR variable expansion will fail if the value contains ! and delayed expansion is enabled.
::WHICH.BAT CommandName [ReturnVar] :: :: Determines the full path of the file that would execute if :: CommandName were executed. :: :: The result is stored in variable ReturnVar, or else it is :: echoed to stdout if ReturnVar is not specified. :: :: If no file is found, then an error message is echoed to stderr. :: :: The ERRORLEVEL is set to one of the following values :: 0 - Success: A matching file was found :: 1 - No file was found and CommandName is an internal command :: 2 - No file was found and CommandName is not an internal command :: 3 - Improper syntax - no CommandName specified :: @echo off setlocal disableDelayedExpansion set "file=%~1" if not defined file ( >&2 echo Syntax error: No CommandName specified exit /b 3 ) set "noExt=" setlocal enableDelayedExpansion if "%~x1" neq "" if "!PATHEXT:%~x1=!" neq "!PATHEXT!" set noExt=""; set "modpath=.\;!PATH!" @for %%E in (%noExt%%PATHEXT%) do @for %%F in ("!file!%%~E") do ( setlocal disableDelayedExpansion if not "%%~$modpath:F"=="" if not exist "%%~$modpath:F\" ( endlocal & endlocal & endlocal if "%~2"=="" (echo %%~$modpath:F) else set "%~2=%%~$modpath:F" exit /b 0 ) endlocal ) endlocal >nul help %~1 && ( >&2 echo "%~1" is not a valid command exit /b 2 ) >&2 echo "%~1" is an internal command
^ Thats a pretty damn good script Dave, thanks for posting it.
I've replaced the script here with Dave's version
Last edited by Simon Sheppard (16 Jun 2012 16:26)