Is there an equivalent of 'which' on the Windows command line?

As I sometimes have path problems, where one of my own cmd scripts is hidden (shadowed) by another program (earlier on the path), I would like to be able to find the full path to a program on the Windows command line, given just its name.

Is there an equivalent to the UNIX command 'which'?

On UNIX, which command prints the full path of the given command to easily find and repair these shadowing problems.


Windows Server 2003 and later (ie anything after Windows XP 32 bit) provide the where.exe program which does some of what which does, though it matches all types of files, not just executable commands. (It does not match built-in shell commands like cd .) It will even accept wildcards, so where nt* finds all files in your %PATH% and current directory whose names start with nt .

Try where /? for help.

Note that Windows PowerShell defines where as an alias for the Where-Object cmdlet, so if you want where.exe , you need to type the full name instead of omitting the .exe extension.


While later versions of Windows have a where command, you can also do this with Windows XP by using the environment variable modifiers, as follows:

c:> for %i in (cmd.exe) do @echo.   %~$PATH:i
   C:WINDOWSsystem32cmd.exe

c:> for %i in (python.exe) do @echo.   %~$PATH:i
   C:Python25python.exe

You don't need any extra tools and it's not limited to PATH since you can substitute any environment variable (in the path format, of course) that you wish to use.


And, if you want one that can handle all the extensions in PATHEXT (as Windows itself does), this one does the trick:

@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

It actually returns all possibilities but you can tweak it quite easily for specific search rules.


Under PowerShell get-command will find executables anywhere in $Env:PATH .

get-command eventvwr

CommandType   Name          Definition
-----------   ----          ----------
Application   eventvwr.exe  c:windowssystem32eventvwr.exe
Application   eventvwr.msc  c:windowssystem32eventvwr.msc

It also finds powershell cmdlets, functions, aliases, files with custom executables extensions via $Env:PATHEXT , etc defined for the current shell (quite akin to bash's type -a foo ) - making it a better go-to than other tools like where.exe , which.exe , etc which are unaware of these PowerShell commands.

You can quickly set up an alias with sal which gcm (short form of set-alias which get-command ).

链接地址: http://www.djcxy.com/p/794.html

上一篇: 使用'for'循环遍历字典

下一篇: 在Windows命令行中是否有相当于'which'的内容?