Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Windows 綜合教學 >> 系統指令

Windows 版 which 指令:批次檔實作在 PATH 中尋找執行檔路徑

編輯:系統指令

Linux which 指令可在 PATH 中尋找命令所在目錄並顯示。Windows NT Resource Kit 有 where.exe 同功能但需付費。本文提供批次檔 which.bat 免費實現此功能。

which.bat 完整程式碼

用記事本建立 which.bat,存放於 Windows 目錄或其他 PATH 目錄:

@echo off
Rem 檢查命令列參數
if {%1}=={} goto :Usage
for %%i in (/? /h -?) do if {%1}==%%i goto :Usage %%i
echo.
:Rem 在當前目錄檢查
for %%i in (%1) do if exist %%~nxi (
    echo %%~fi
    goto :EOF
)
:Rem 在 PATH 中檢查
for %%i in (%1) do (
    if exist %%~$PATH:i (
        echo %%~$PATH:i
    ) else (
        echo Error: %1 不在路徑裡!!!
        goto :Usage
    )
)
goto :EOF
:Usage
echo.
echo WHICH "filename"
echo.
echo 輸入檔名,返回該檔案的完整路徑!
echo.

測試結果

C:\>which
WHICH "filename"
輸入檔名,返回該檔案的完整路徑!

C:\>which cmd.exe
C:\WINNT\system32\cmd.exe
OK,測試通過!

將此批次檔放入系統路徑,即可在 Windows 享受 which 便利。

Copyright © Windows教程網 All Rights Reserved