雖然以后版本的Windows有一個(gè)where
命令,您也可以通過(guò)使用環(huán)境變量修飾符來(lái)完成此操作,如下所示:
c:\> for %i in (cmd.exe) do @echo. %~$PATH:i
C:\WINDOWS\system32\cmd.exe
c:\> for %i in (python.exe) do @echo. %~$PATH:i
C:\Python25\python.exe
你不需要任何額外的工具,而且它不限于PATH
因?yàn)槟梢蕴鎿Q您希望使用的任何環(huán)境變量(當(dāng)然是路徑格式)。
而且,如果您想要一個(gè)能夠處理PATHEXT中的所有擴(kuò)展(就像Windows本身一樣)的擴(kuò)展,那么這個(gè)擴(kuò)展就可以實(shí)現(xiàn):
@echo off
setlocal enableextensions enabledelayedexpansion
:: Needs an argument.
if "x%1"=="x" (
echo Usage: which ^<progName^>
goto :end
)
:: First try the unadorned filenmame.
set fullspec=
call :find_it %1
:: Then try all adorned filenames in order.
set mypathext=!pathext!
:loop1
:: Stop if found or out of extensions.
if "x!mypathext!"=="x" goto :loop1end
:: Get the next extension and try it.
for /f "delims=;" %%j in ("!mypathext!") do set myext=%%j
call :find_it %1!myext!
:: Remove the extension (not overly efficient but it works).
:loop2
if not "x!myext!"=="x" (
set myext=!myext:~1!
set mypathext=!mypathext:~1!
goto :loop2
)
if not "x!mypathext!"=="x" set mypathext=!mypathext:~1!
goto :loop1
:loop1end
:end
endlocal
goto :eof
:: Function to find and print a file in the path.
:find_it
for %%i in (%1) do set fullspec=%%~$PATH:i
if not "x!fullspec!"=="x" @echo. !fullspec!
goto :eof
它實(shí)際上返回了所有的可能性,但是您可以很容易地根據(jù)特定的搜索規(guī)則對(duì)其進(jìn)行調(diào)整。