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

问题描述 投票:116回答:3

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

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

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

detect_program_path X.EXE
windows command-line path
3个回答
210
投票

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

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

根据this blog post的说法,where.exe包含在Windows Server 2003及更高版本中,所以这应该适用于Vista,Win 7等。

在Linux上,等效的是which命令,例如which ssh


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

3
投票

正如评论中提到的thread,powershell中的get-command也可以解决它。例如,您可以键入get-command npm,输出如下:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.