Created
November 18, 2013 14:39
-
-
Save eyecatchup/7528866 to your computer and use it in GitHub Desktop.
`which` for Win
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
::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 | |
:: | |
:: Source: http://ss64.org/viewtopic.php?pid=5752#p5752 | |
:: | |
@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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment