查找将从 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:41:14  来源:igfitidea点击:

Finding the path of the program that will execute from the command line in Windows

windowscommand-linepath

提问by Zabba

Say I have a program X.EXEinstalled 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.EXEwhich 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 wherecommand. 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.exeis 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 whichcommand, 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

回答by Eugene

As the threadmentioned in the comment, get-commandin powershell can also work it out. For example, you can type get-command npmand the output is as below:

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

enter image description here

在此处输入图片说明