为了账号安全,请及时绑定邮箱和手机立即绑定

Windows命令行中是否有“哪个”的等价物?

Windows命令行中是否有“哪个”的等价物?

守候你守候我 2019-06-13 19:11:57
Windows命令行中是否有“哪个”的等价物?由于有时我有路径问题,其中一个自己的cmd脚本被另一个程序隐藏(隐藏在路径的前面),所以我希望能够在Windows命令行中找到一个程序的完整路径,只要给出它的名称。是否与UNIX命令“哪个”等效?在UNIX上,which command打印给定命令的完整路径,以便轻松查找和修复这些隐藏问题。
查看完整描述

3 回答

?
开心每一天1111

TA贡献1836条经验 获得超13个赞

虽然以后版本的Windows有一个where命令,您也可以通过使用环境变量修饰符来完成此操作,如下所示:

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

它实际上返回了所有的可能性,但是您可以很容易地根据特定的搜索规则对其进行调整。


查看完整回答
反对 回复 2019-06-13
  • 3 回答
  • 0 关注
  • 663 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信