查找将从 Windows 中的命令行执行的程序的路径

假设我在系统的 c:\abcd\happy\文件夹中安装了一个程序 X.EXE。该文件夹位于系统路径上。现在假设系统上有另一个程序也叫做 X.EXE,但是安装在文件夹 c:\windows\中。

是否有可能从命令行中快速查明,如果我键入 X.EXE,两个 X.EXE中的哪一个将被启动?(但不必在任务管理器中进行目录搜索或查看流程细节)。

也许是某种内置的命令,或者某个程序可以做这样的事情? :

detect_program_path X.EXE
133054 次浏览

使用 where命令。列表中的第一个结果是将要执行的结果。

C:\> where notepad
C:\Windows\System32\notepad.exe
C:\Windows\notepad.exe

根据 这篇博文where.exe是包含在 WindowsServer2003和更高版本,所以这应该只与 Vista,Win7等工作。

在 Linux 上,相当于 which命令,例如 which ssh

下面是一个小的 cmd 脚本,你可以将它复制粘贴到一个类似 where.cmd的文件中:

@echo off
rem - search for the given file in the directories specified by the path, and display the first match
rem
rem    The main ideas for this script were taken from Raymond Chen's blog:
rem
rem         http://blogs.msdn.com/b/oldnewthing/archive/2005/01/20/357225.asp
rem
rem
rem - it'll be nice to at some point extend this so it won't stop on the first match. That'll
rem     help diagnose situations with a conflict of some sort.
rem


setlocal


rem - search the current directory as well as those in the path
set PATHLIST=.;%PATH%
set EXTLIST=%PATHEXT%


if not "%EXTLIST%" == "" goto :extlist_ok
set EXTLIST=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
:extlist_ok


rem - first look for the file as given (not adding extensions)
for %%i in (%1) do if NOT "%%~$PATHLIST:i"=="" echo %%~$PATHLIST:i


rem - now look for the file adding extensions from the EXTLIST
for %%e in (%EXTLIST%) do @for %%i in (%1%%e) do if NOT "%%~$PATHLIST:i"=="" echo %%~$PATHLIST:i

正如注释中提到的 线,Powershell 中的 get-command也可以解决这个问题。例如,您可以键入 get-command npm,输出如下:

enter image description here