查找将从 Windows 中的命令行执行的程序的路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4002819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding the path of the program that will execute from the command line in Windows
提问by Zabba
Say I have a program X.EXE
installed in folder c:\abcd\happy\
on the system. The folder is on the system path. Now suppose there is another program on the system that's also called X.EXE but is installed in folder c:\windows\
.
假设我在系统上的X.EXE
文件夹中安装了一个程序c:\abcd\happy\
。该文件夹位于系统路径上。现在假设系统上还有另一个程序,也称为 X.EXE,但安装在文件夹 中c:\windows\
。
Is it possible to quickly find out from the command line that if I type in X.EXE
which of the two X.EXE
's will get launched? (but without having to dir search or look at the process details in Task Manager).
是否可以从命令行中快速找出如果我输入X.EXE
这两个中的哪一个X.EXE
将被启动?(但不必直接搜索或查看任务管理器中的进程详细信息)。
Maybe some sort of in-built command, or some program out there that can do something like this? :
也许某种内置命令,或者一些可以做这样的事情的程序?:
detect_program_path X.EXE
回答by Chris Schmich
Use the where
command. The first result in the list is the one that will execute.
使用where
命令。列表中的第一个结果是将执行的结果。
C:\> where notepad C:\Windows\System32\notepad.exe C:\Windows\notepad.exe
According to this blog post, where.exe
is included with Windows Server 2003 and later, so this should just work with Vista, Win 7, et al.
根据这篇博客文章,where.exe
包含在 Windows Server 2003 及更高版本中,因此这应该只适用于 Vista、Win 7 等。
On Linux, the equivalent is the which
command, e.g. which ssh
.
在 Linux 上,等效的是which
命令,例如which ssh
.
回答by Michael Burr
Here's a little cmd script you can copy-n-paste into a file named something like where.cmd
:
这是一个小的 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