2012-06-13 17 views
29

Chciałbym dowiedzieć się, która wersja pliku wykonywalnego używa powłoki CMD. W dowolnej powłoce uniksowej użyłbym which, aby ją znaleźć.Co odpowiada cmd/powershell `which` na bash?

Czy istnieje odpowiednik polecenia w jednej z powłok systemu Windows?

+0

Tak, istnieje http://stackoverflow.com/questions/304319/is-there-an-equivalent-of-which-on-windows/304441#304441 – paxdiablo

Odpowiedz

50

Różne.

  1. where jest bezpośrednim odpowiednikiem:

    C:\Users\Joey>where cmd 
    C:\Windows\System32\cmd.exe 
    

    Należy zauważyć, że w PowerShell where sama jest aliasem dla Where-Object, więc trzeba użyć where.exe w PowerShell.

  2. W cmd można również użyć for:

    C:\Users\Joey>for %x in (powershell.exe) do @echo %~$PATH:x 
    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe 
    
  3. PowerShell masz Get-Command i jej alias gcm który robi to samo, jeśli przechodzą argument (ale działa również dla aliasów poleceń cmdlet i funkcji PowerShell):

    PS C:\Users\Joey> Get-Command where 
    
    CommandType  Name   Definition 
    -----------  ----   ---------- 
    Alias   where   Where-Object 
    Application  where.exe  C:\Windows\system32\where.exe 
    

    Pierwsze zwrócone polecenie to polecenie, które zostanie wykonane.

+1

Z jakiegoś powodu "gdzie" nie działa dla mnie w ogóle (nic nie wydrukowałem i od razu rzuciłem), ale 'gcm' działało świetnie. – Hassan

+1

@Hassan: Jak można zebrać z przykładowego wyjścia 'Get-Command gdzie' na PowerShellu,' where' jest tak naprawdę aliasem dla 'Where-Object', który ma pierwszeństwo przed' where.exe'. Aby uruchomić 'where.exe', musisz wpisać' where.exe'. Przynajmniej w PowerShell. – Joey

+0

Oh. Tak, to był problem. Dzięki! – Hassan

4

Komenda WHERE niekoniecznie jest takie samo jak UNIX which, ponieważ zawiera wszystkie pasujące pliki znajdujące się w bieżącym katalogu lub ścieżki. Jak mówi Joey, pierwsza wymieniona to ta, którą wykonuje. Łatwo jest stworzyć skrypt wsadowy, który zwróci tylko pierwszy znaleziony.

@echo off 
for /f "delims=" %%F in ('where %1') do (
    echo %%F 
    exit /b 
) 

Ale WHERE jest stosunkowo wolny.

Poniżej znajduje się skrypt WHICH.BAT, który jest szybszy i robi nieco więcej. Używa rozszerzonego rozszerzonego przełączania, ponieważ: 1) Rozszerzenie% PATH% jest niewiarygodne, jeśli istnieją niecytowane znaki specjalne. 2) Rozszerzanie dla zmiennych, gdy włączone jest opóźnione rozwijanie, powoduje uszkodzenie wartości, które zawierają !.

::WHICH.BAT CommandName [ReturnVar] 
:: 
:: Determines the full path of the file that would execute if 
:: CommandName were executed. 
:: 
:: The result is stored in variable ReturnVar, or else it is 
:: echoed to stdout if ReturnVar is not specified. 
:: 
:: If no file is found, then an error message is echoed to stderr. 
:: 
:: The ERRORLEVEL is set to one of the following values 
:: 0 - Success: A matching file was found 
:: 1 - CommandName is an internal command 
:: 2 - No file was found and CommandName is not an internal command 
:: 3 - Improper syntax - no CommandName specified 
:: 
@echo off 
setlocal disableDelayedExpansion 

set "file=%~1" 
setlocal enableDelayedExpansion 

if not defined file (
    >&2 echo Syntax error: No CommandName specified 
    exit /b 3 
) 


:: test for internal command 
echo(!file!|findstr /i "[^abcdefghijklmnopqrstuvwxyz]" >nul || (
    set "empty=!temp!\emptyFolder" 
    md "!empty!" 2>nul 
    del /q "!empty!\*" 2>nul >nul 
    setlocal 
    pushd "!empty!" 
    set path= 
    (call) 
    !file! /? >nul 2>nul 
    if not errorlevel 9009 (
    >&2 echo "!file!" is an internal command 
    popd 
    exit /b 1 
) 
    popd 
    endlocal 
) 


:: test for external command 
set "noExt=" 
if "%~x1" neq "" if "!PATHEXT:%~x1=!" neq "!PATHEXT!" set noExt=""; 
set "modpath=.\;!PATH!" 
@for %%E in (%noExt%%PATHEXT%) do @for %%F in ("!file!%%~E") do (
    setlocal disableDelayedExpansion 
    if not "%%~$modpath:F"=="" if not exist "%%~$modpath:F\" (
    endlocal & endlocal & endlocal 
    if "%~2"=="" (echo %%~$modpath:F) else set "%~2=%%~$modpath:F" 
    exit /b 0 
) 
    endlocal 
) 
endlocal 


>&2 echo "%~1" is not a valid command 
exit /b 2 

UPDATE

musiałem znacznie zmodyfikować skrypt powyżej, ponieważ został nieprawidłowo wystawianie polecenie wewnętrzne jak zewnętrzne, jeśli nie stało istnieć plik exe o tej samej nazwie korzeni gdzieś w PATH.