由于我有时会遇到路径问题,我自己的一个cmd脚本被另一个程序隐藏(隐藏)(路径前面),所以我希望能够在Windows命令行上找到程序的完整路径,只要给出它的名称。
是否有与UNIX命令“which”等效的命令?
在UNIX上,哪个命令打印给定命令的完整路径,以便轻松查找和修复这些阴影问题。
由于我有时会遇到路径问题,我自己的一个cmd脚本被另一个程序隐藏(隐藏)(路径前面),所以我希望能够在Windows命令行上找到程序的完整路径,只要给出它的名称。
是否有与UNIX命令“which”等效的命令?
在UNIX上,哪个命令打印给定命令的完整路径,以便轻松查找和修复这些阴影问题。
当前回答
只需发布此Windows的单行批处理文件:
C:>type wh.cmd
@for %%f in (%*) do for %%e in (%PATHEXT% .dll .lnk) do for %%b in (%%f%%e) do for %%d in (%PATH%) do if exist %%d\%%b echo %%d\%%b
A测试:
C:>wh ssh
C:\cygwin64\bin\ssh.EXE
C:\Windows\System32\OpenSSH\\ssh.EXE
如果将代码包装在setlocalenableextensions和endlocal中,那就不是一行了。
其他回答
虽然较新版本的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中,它是gcm,它提供有关其他命令的格式化信息。如果只想检索可执行文件的路径,请使用.Source。
例如:gcm-git或(gcm-git).Source
提示:
适用于Windows XP。自PowerShell 1.0之后可用。gcm是Get-Command cmdlet的别名。在没有任何参数的情况下,它列出了主机shell提供的所有可用命令。您可以使用Set alias which gcm创建自定义别名,并使用它,例如:(which git).Source。官方文件:https://technet.microsoft.com/en-us/library/ee176842.aspx
在调用以下内容的Windows CMD中:
$ where php
C:\Program Files\PHP\php.exe
对于Windows XP用户(他们没有内置where命令),我编写了一个“where like”命令作为一个名为whicher的rubygem。
要安装它,请安装Ruby。
Then
gem install whichr
运行方式如下:
C: >其中的命令
GnuWin32工具以及一系列其他Unix工具都有。