2010-10-23 79 views
95

假设我在系统上安装了文件夹c:\abcd\happy\。该文件夹位于系统路径中。现在假设系统上还有另一个程序,也称为X.EXE,但安装在文件夹c:\windows\中。查找将从Windows命令行执行的程序的路径

是否有可能从命令行快速找到,如果我输入X.EXE两个X.EXE的哪一个会启动? (但无需在任务管理器中查找或查看流程细节)。

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

detect_program_path X.EXE 
+0

的可能的复制[是否有的“其中” Windows命令行上的等效?](http://stackoverflow.com/问题/ 304319/is-there-an-equivalent-of-the-windows-command-line) – 2017-04-06 07:23:02

回答

171

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

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

this blog postwhere.exe被包含在Windows Server 2003及更高版本,所以这应该只是与Vista,Win 7的,等工作。

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

+2

+1!我从来不知道这可能是Windows的一部分,所以没有朝这个方向看! :) – Zabba 2010-10-23 06:45:29

+1

穷人xp用户的任何等价物? – 2011-04-28 12:23:27

+0

@shahar_m:你是否尝试过Michael Burr下面的脚本?它不是内置的,但它可以做你需要的。 – 2011-04-28 17:19:35

9

这里有一个小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 
0

正如在评论中提到的threadget-command在PowerShell中也做得出来。例如,可以键入get-command npm并且输出是如下:

enter image description here