由于我有时遇到路径问题,其中一个我自己的cmd脚本被另一个程序隐藏(阴影)(在路径的前面),我希望能够在Windows命令行上找到程序的完整路径,给定只是它的名字。
有没有相当于UNIX命令'哪个'?
在UNIX上, which command
打印给定命令的完整路径,以便轻松查找和修复这些阴影问题。
由于我有时遇到路径问题,其中一个我自己的cmd脚本被另一个程序隐藏(阴影)(在路径的前面),我希望能够在Windows命令行上找到程序的完整路径,给定只是它的名字。
有没有相当于UNIX命令'哪个'?
在UNIX上, which command
打印给定命令的完整路径,以便轻松查找和修复这些阴影问题。
Windows Server 2003及更高版本(即Windows XP 32位之后的任何内容)提供了 where.exe
做一些什么的程序 which
虽然它匹配所有类型的文件,但不仅仅是可执行命令。 (它与内置的shell命令不匹配 cd
。)它甚至会接受通配符,所以 where nt*
找到你的所有文件 %PATH%
和名称以。开头的当前目录 nt
。
尝试 where /?
求助。
请注意,Windows PowerShell定义 where
作为别名 该 Where-Object
小命令,所以如果你想 where.exe
,你需要输入全名而不是省略 .exe
延期。
虽然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
您不需要任何额外的工具,但不限于此 PATH
因为您可以替换您希望使用的任何环境变量(当然是路径格式)。
并且,如果你想要一个可以处理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
它实际上返回了所有可能性,但您可以轻松地针对特定搜索规则进行调整。
在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
它还可以找到powershell cmdlet,函数,别名,带有自定义可执行文件扩展名的文件 $Env:PATHEXT
等等为当前shell定义(非常类似于bash的
type -a foo
) - 使它成为比其他工具更好的选择 where.exe
, which.exe
等不知道这些PowerShell命令。
您可以使用快速设置别名 sal which gcm
(简称
set-alias which get-command
)。
在windows powershell中:
set-alias which where.exe
如果您安装了PowerShell(我推荐),您可以使用以下命令作为粗略的等效项(替换程序名为您的可执行文件的名称):
($Env:Path).Split(";") | Get-ChildItem -filter programName*
更多信息: http://www.codeassassin.com/blog/PermaLink,guid,fd1967d1-f844-4e29-82e2-f2d6424b4ef9.aspx
该 的GnuWin32 工具有 which
,以及一大堆其他Unix工具。
在Windows CMD中 which
电话 where
:
$ where php
C:\Program Files\PHP\php.exe
很惊讶,没有人提到过 cygwin的 作为一种解决方案。如果您不介意使用第三方解决方案,那么cygwin就是您的选择。
Cygwin的 在Windows环境中为您提供* nix的舒适度(您可以在Windows命令shell中使用它,或使用您选择的* nix shell)。它为您提供了大量的* nix命令(如 which
)对于Windows,你可以在你的目录中包含该目录 PATH
。
在PowerShell中,它是 gcm
,提供有关其他命令的格式化信息。如果要仅检索可执行文件的路径,请使用 .Source
。
例如: gcm git
要么 (gcm git).Source
小知识:
gcm
是别的 Get-Command
小命令。Set-Alias which gcm
并使用它像: (which git).Source
。从这里获取unxutils: http://sourceforge.net/projects/unxutils/
Windows平台上的黄金,将所有漂亮的unix实用程序放在标准的Windows DOS上。多年来一直在使用它。
它有一个'包括'。请注意,它虽然区分大小写。
注意:安装它会在某处爆炸zip并将... \ UnxUtils \ usr \ local \ wbin \添加到系统路径env变量中。