Windows 命令行上是否有等价于“which”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/304319/
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 05:45:30  来源:igfitidea点击:

Is there an equivalent of 'which' on the Windows command line?

windowscommand-linepath-variables

提问by namin

As I sometimes have path problems, where one of my own cmd scripts is hidden (shadowed) by another program (earlier on the path), I would like to be able to find the full path to a program on the Windows command line, given just its name.

由于我有时会遇到路径问题,其中我自己的一个 cmd 脚本被另一个程序(路径上的早期)隐藏(隐藏),我希望能够在 Windows 命令行上找到程序的完整路径,给定只是它的名字。

Is there an equivalent to the UNIX command 'which'?

是否有与 UNIX 命令“which”等效的命令?

On UNIX, which commandprints the full path of the given command to easily find and repair these shadowing problems.

在 UNIX 上,which command打印给定命令的完整路径以轻松查找和修复这些阴影问题。

回答by Michael Ratanapintha

Windows Server 2003 and later (i.e. anything after Windows XP 32 bit) provide the where.exeprogram which does some of what whichdoes, though it matches all types of files, not just executable commands. (It does not match built-in shell commands like cd.) It will even accept wildcards, so where nt*finds all files in your %PATH%and current directory whose names start with nt.

Windows Server 2003 及更高版本(即 Windows XP 32 位之后的任何内容)提供了where.exe执行某些功能的程序which,尽管它匹配所有类型的文件,而不仅仅是可执行命令。(它不匹配内置的 shell 命令,比如cd。)它甚至会接受通配符,因此where nt*在您的%PATH%和当前目录中查找名称以nt.

Try where /?for help.

尝试where /?寻求帮助。

Note that Windows PowerShell defines whereas an alias for the Where-Objectcmdlet, so if you want where.exe, you need to type the full name instead of omitting the .exeextension.

请注意,Windows PowerShell中定义where为一个别名Where-Objectcmdlet的,所以如果你想where.exe,你需要输入完整的名称,而不是省略的.exe扩展。

回答by paxdiablo

While later versions of Windows have a wherecommand, you can also do this with Windows XP by using the environment variable modifiers, as follows:

虽然更高版本的 Windows 有一个where命令,但您也可以通过使用环境变量修饰符在 Windows XP 中执行此操作,如下所示:

c:\> for %i in (cmd.exe) do @echo.   %~$PATH:i
   C:\WINDOWS\system32\cmd.exe

c:\> for %i in (python.exe) do @echo.   %~$PATH:i
   C:\Python25\python.exe

You don't need any extra tools and it's not limited to PATHsince you can substitute any environment variable (in the path format, of course) that you wish to use.

您不需要任何额外的工具,而且不限于此,PATH因为您可以替换您希望使用的任何环境变量(当然是路径格式)。



And, if you want one that can handle all the extensions in PATHEXT (as Windows itself does), this one does the trick:

而且,如果你想要一个可以处理 PATHEXT 中所有扩展的(就像 Windows 本身一样),这个就可以了:

@echo off
setlocal enableextensions enabledelayedexpansion

:: Needs an argument.

if "x%1"=="x" (
    echo Usage: which ^<progName^>
    goto :end
)

:: First try the unadorned filenmame.

set fullspec=
call :find_it %1

:: Then try all adorned filenames in order.

set mypathext=!pathext!
:loop1
    :: Stop if found or out of extensions.

    if "x!mypathext!"=="x" goto :loop1end

    :: Get the next extension and try it.

    for /f "delims=;" %%j in ("!mypathext!") do set myext=%%j
    call :find_it %1!myext!

:: Remove the extension (not overly efficient but it works).

:loop2
    if not "x!myext!"=="x" (
        set myext=!myext:~1!
        set mypathext=!mypathext:~1!
        goto :loop2
    )
    if not "x!mypathext!"=="x" set mypathext=!mypathext:~1!

    goto :loop1
:loop1end

:end
endlocal
goto :eof

:: Function to find and print a file in the path.

:find_it
    for %%i in (%1) do set fullspec=%%~$PATH:i
    if not "x!fullspec!"=="x" @echo.   !fullspec!
    goto :eof

It actually returns all possibilities but you can tweak it quite easily for specific search rules.

它实际上返回所有可能性,但您可以针对特定搜索规则轻松调整它。

回答by shalomb

Under PowerShell, Get-Commandwill find executables anywhere in $Env:PATH.

在 PowerShell 下,Get-Command将在$Env:PATH.

Get-Command eventvwr

CommandType   Name          Definition
-----------   ----          ----------
Application   eventvwr.exe  c:\windows\system32\eventvwr.exe
Application   eventvwr.msc  c:\windows\system32\eventvwr.msc

It also finds PowerShell cmdlets, functions, aliases, files with custom executables extensions via $Env:PATHEXT, etc. defined for the current shell (quite akin to Bash's type -a foo) - making it a better go-to than other tools like where.exe, which.exe, etc which are unaware of these PowerShell commands.

它还发现PowerShell命令,函数,别名文件通过自定义的可执行文件的扩展$Env:PATHEXT等,为当前shell(相当类似于bash的定义type -a foo) -使之更好地去到比其他工具一样where.exewhich.exe等它不知道这些PowerShell 命令。

Finding executables using only part of the name

仅使用名称的一部分查找可执行文件

gcm *disk*

CommandType     Name                             Version    Source
-----------     ----                             -------    ------
Alias           Disable-PhysicalDiskIndication   2.0.0.0    Storage
Alias           Enable-PhysicalDiskIndication    2.0.0.0    Storage
Function        Add-PhysicalDisk                 2.0.0.0    Storage
Function        Add-VirtualDiskToMaskingSet      2.0.0.0    Storage
Function        Clear-Disk                       2.0.0.0    Storage
Cmdlet          Get-PmemDisk                     1.0.0.0    PersistentMemory
Cmdlet          New-PmemDisk                     1.0.0.0    PersistentMemory
Cmdlet          Remove-PmemDisk                  1.0.0.0    PersistentMemory
Application     diskmgmt.msc                     0.0.0.0    C:\WINDOWS\system32\diskmgmt.msc
Application     diskpart.exe                     10.0.17... C:\WINDOWS\system32\diskpart.exe
Application     diskperf.exe                     10.0.17... C:\WINDOWS\system32\diskperf.exe
Application     diskraid.exe                     10.0.17... C:\WINDOWS\system32\diskraid.exe
...

Finding custom executables

查找自定义可执行文件

To find other non-windows executables (python, ruby, perl, etc), file extensions for those executables need to be added to the PATHEXTenvironmental variable (defaults to .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL) to identify files with these extensions in the PATHas executable. As Get-Commandalso honours this variable, it can be extended to list custom executables. e.g.

要查找其他非 Windows 可执行文件(python、ruby、perl 等),需要将这些可执行文件的文件扩展名添加到PATHEXT环境变量(默认为.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL),以在PATHas 可执行文件中识别具有这些扩展名的文件。由于Get-Command还表彰这个变量,它可以扩展到列表中的自定义可执行文件。例如

$Env:PATHEXT="$Env:PATHEXT;.dll;.ps1;.psm1;.py"     # temporary assignment, only for this shell's process

gcm user32,kernel32,*WASM*,*http*py

CommandType     Name                        Version    Source
-----------     ----                        -------    ------
ExternalScript  Invoke-WASMProfiler.ps1                C:\WINDOWS\System32\WindowsPowerShell\v1.0\Invoke-WASMProfiler.ps1
Application     http-server.py              0.0.0.0    C:\Users\ME\AppData\Local\Microsoft\WindowsApps\http-server.py
Application     kernel32.dll                10.0.17... C:\WINDOWS\system32\kernel32.dll
Application     user32.dll                  10.0.17... C:\WINDOWS\system32\user32.dll

You can quickly set up an alias with sal which gcm(short form of set-alias which get-command).

您可以使用sal which gcm( 的缩写形式set-alias which get-command)快速设置别名。

More information and examples can be found under the online help for Get-Command.

更多信息和示例可以在 的在线帮助下找到Get-Command

回答by cmcginty

In Windows PowerShell:

在 Windows PowerShell 中:

set-alias which where.exe

回答by RexE

If you have PowerShell installed (which I recommend), you can use the following command as a rough equivalent (substitute programName for your executable's name):

如果您安装了 PowerShell(我推荐),您可以使用以下命令作为粗略的等效命令(用 programName 替换您的可执行文件的名称):

($Env:Path).Split(";") | Get-ChildItem -filter programName*

More is here: My Manwich! PowerShell Which

更多在这里: 我的曼威奇!PowerShell 哪个

回答by Ferruccio

The GnuWin32tools have which, along with a whole slew of other Unix tools.

的GnuWin32工具有which,与其他Unix工具整体转换一起。

回答by automatix

In Windows CMD whichcalls where:

在 Windows CMDwhich调用中where

$ where php
C:\Program Files\PHP\php.exe

回答by palswim

Cygwinis a solution. If you don't mind using a third-party solution, then Cygwin is the way to go.

Cygwin是一个解决方案。如果您不介意使用第三方解决方案,那么 Cygwin 是您的最佳选择。

Cygwin gives you the comfort of *nix in the Windows environment (and you can use it in your Windows command shell, or use a *nix shell of your choice). It gives you a whole host of *nix commands (like which) for Windows, and you can just include that directory in your PATH.

Cygwin 在 Windows 环境中为您提供了舒适的 *nix(您可以在 Windows 命令外壳中使用它,或者使用您选择的 *nix 外壳)。它为您提供了一整套which适用于 Windows的 *nix 命令(如),您只需将该目录包含在您的PATH.

回答by vulcan raven

In PowerShell, it is gcm, which gives formatted information about other commands. If you want to retrieve only path to executable, use .Source.

在 PowerShell 中,它是gcm,它提供有关其他命令的格式化信息。如果您只想检索可执行文件的路径,请使用.Source.

For instance: gcm gitor (gcm git).Source

例如:gcm git(gcm git).Source

Tidbits:

花絮:

回答by Fez

I have a function in my PowerShell profile named 'which'

我的 PowerShell 配置文件中有一个名为“which”的函数

function which {
    get-command $args[0]| format-list
}

Here's what the output looks like:

输出如下所示:

PS C:\Users\fez> which python


Name            : python.exe
CommandType     : Application
Definition      : C:\Python27\python.exe
Extension       : .exe
Path            : C:\Python27\python.exe
FileVersionInfo : File:             C:\Python27\python.exe
                  InternalName:
                  OriginalFilename:
                  FileVersion:
                  FileDescription:
                  Product:
                  ProductVersion:
                  Debug:            False
                  Patched:          False
                  PreRelease:       False
                  PrivateBuild:     False
                  SpecialBuild:     False
                  Language: