由于我有时会遇到路径问题,我自己的一个cmd脚本被另一个程序隐藏(隐藏)(路径前面),所以我希望能够在Windows命令行上找到程序的完整路径,只要给出它的名称。
是否有与UNIX命令“which”等效的命令?
在UNIX上,哪个命令打印给定命令的完整路径,以便轻松查找和修复这些阴影问题。
由于我有时会遇到路径问题,我自己的一个cmd脚本被另一个程序隐藏(隐藏)(路径前面),所以我希望能够在Windows命令行上找到程序的完整路径,只要给出它的名称。
是否有与UNIX命令“which”等效的命令?
在UNIX上,哪个命令打印给定命令的完整路径,以便轻松查找和修复这些阴影问题。
当前回答
在Windows PowerShell中:
set-alias which where.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
它实际上返回所有可能性,但您可以很容易地针对特定搜索规则进行调整。
此批处理文件使用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%)
参见集合/?寻求帮助。
可以下载为Windows编译的所有UNIX命令,包括从GitHub存储库下载的命令:https://github.com/George-Ogden/UNIX
Windows没有现货,但它是由Services for Unix提供的,有几个简单的批处理脚本可以完成相同的任务,比如这一个。
试试这个
set a=%~$dir:1
If "%for%"=="" (Echo %1 not found) else (echo %1 found at %a%)