Der WHERE
Befehl ist nicht ganz dasselbe wie Unix which
, weil es alle passenden Dateien im aktuellen Verzeichnis oder PATH aufgelistet sind. Wie Joey sagt, ist der Erste, der aufgeführt wird, der, der ausführt. Es ist einfach, ein Batch-Skript zu erstellen, das nur das erste gefundene Skript zurückgibt.
@echo off
for /f "delims=" %%F in ('where %1') do (
echo %%F
exit /b
)
Aber WHERE
ist relativ langsam.
Unten ist ein WHICH.BAT Skript, das schneller ist und ein bisschen mehr tut. Es verwendet umfangreiche verzögerte Expansions-Toggelung, weil: 1)% PATH% erweitern ist unzuverlässig, wenn es nicht aufgezählte Sonderzeichen gibt. 2) Durch das Erweitern von FOR-Variablen bei aktivierter verzögerter Erweiterung werden Werte korrumpiert, die !
enthalten.
::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
ich deutlich das Skript ändern musste oben, weil es als externes einen internen Befehl falsch Auflistung, wenn es in der PATH eine exe-Datei mit dem gleichen Stammnamen irgendwo zufällig existieren.
Ja, es gibt http://StackOverflow.com/Questions/304319/is-there-an-equivalent-of-which-on-windows/304441#304441 – paxdiablo