由于我有时会遇到路径问题,我自己的一个cmd脚本被另一个程序隐藏(隐藏)(路径前面),所以我希望能够在Windows命令行上找到程序的完整路径,只要给出它的名称。
是否有与UNIX命令“which”等效的命令?
在UNIX上,哪个命令打印给定命令的完整路径,以便轻松查找和修复这些阴影问题。
由于我有时会遇到路径问题,我自己的一个cmd脚本被另一个程序隐藏(隐藏)(路径前面),所以我希望能够在Windows命令行上找到程序的完整路径,只要给出它的名称。
是否有与UNIX命令“which”等效的命令?
在UNIX上,哪个命令打印给定命令的完整路径,以便轻松查找和修复这些阴影问题。
当前回答
Cygwin是一个解决方案。如果您不介意使用第三方解决方案,那么Cygwin就是您的选择。
Cygwin在Windows环境中为您提供了*nix的舒适性(您可以在Windows命令shell中使用它,也可以选择使用*nix shell)。它为您提供了一整套适用于Windows的*nix命令(如),您可以将该目录包含在PATH中。
其他回答
虽然较新版本的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(我建议使用),可以使用以下命令作为大致等效命令(用programName替换可执行文件的名称):
($Env:Path).Split(";") | Get-ChildItem -filter programName*
更多信息:我的曼维奇!PowerShell哪个
我已经使用npm中的which模块很长时间了,它工作得很好:https://www.npmjs.com/package/which这是一个很好的多平台替代方案。
现在我切换到Git附带的。只需将Git中的/usr/bin路径添加到路径中,该路径通常位于C:\Program Files\Git\usr\bin\which.exe。哪个二进制文件位于C:\Program Files \Git\usr/bin\which.exe。它速度更快,也能按预期工作。
对于Windows XP用户(他们没有内置where命令),我编写了一个“where like”命令作为一个名为whicher的rubygem。
要安装它,请安装Ruby。
Then
gem install whichr
运行方式如下:
C: >其中的命令
此批处理文件使用CMD变量处理来查找将在路径中执行的命令。注意:当前目录总是在路径之前完成),并且根据使用的API调用,在路径之前/之后搜索其他位置。
@echo off
echo.
echo PathFind - Finds the first file in in a path
echo ======== = ===== === ===== ==== == == = ====
echo.
echo Searching for %1 in %path%
echo.
set a=%~$PATH:1
If "%a%"=="" (Echo %1 not found) else (echo %1 found at %a%)
参见集合/?寻求帮助。