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

后端 未结 26 2049
悲哀的现实
悲哀的现实 2020-11-22 00:40

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 pa

相关标签:
26条回答
  • 2020-11-22 00:58

    It is possible to download all of the UNIX commands compiled for Windows, including which from this GitHub repository: https://github.com/George-Ogden/UNIX

    0 讨论(0)
  • 2020-11-22 01:00

    I am using GOW (GNU on Windows) which is a light version of Cygwin. You can grab it from GitHub here.

    GOW (GNU on Windows) is the lightweight alternative to Cygwin. It uses a convenient Windows installer that installs about 130 extremely useful open source UNIX applications compiled as native win32 binaries. It is designed to be as small as possible, about 10 MB, as opposed to Cygwin which can run well over 100 MB depending upon options. - About Description(Brent R. Matzelle)

    A screenshot of a list of commands included in GOW:

    0 讨论(0)
  • 2020-11-22 01:01

    In Windows PowerShell:

    set-alias which where.exe
    
    0 讨论(0)
  • 2020-11-22 01:04

    Cygwin is a solution. If you don't mind using a third-party solution, then Cygwin is the way to go.

    Cygwin gives you the comfort of *nix in the Windows environment (and you can use it in your Windows command shell, or use a *nix shell of your choice). It gives you a whole host of *nix commands (like which) for Windows, and you can just include that directory in your PATH.

    0 讨论(0)
  • 2020-11-22 01:05

    try this

    set a=%~$dir:1
    If "%for%"=="" (Echo %1 not found) else (echo %1 found at %a%)
    
    0 讨论(0)
  • 2020-11-22 01:06

    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:\WINDOWS\system32\cmd.exe
    
    c:\> for %i in (python.exe) do @echo.   %~$PATH:i
       C:\Python25\python.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.

    0 讨论(0)
提交回复
热议问题