(view source code of which.bat as plain text)
@ECHO OFF
:: Check Windows version
IF "%OS%"=="Windows_NT" (SETLOCAL) ELSE (GOTO Syntax)
:: Check command line argument
IF "%~1"=="" GOTO Syntax
IF NOT "%~2"=="" GOTO Syntax
ECHO.%1 | FIND /V ":" | FIND /V "\" | FIND /V "*" | FIND /V "?" | FIND /V "," | FIND /V ";" | FIND /V "/" | FIND "%~1" >NUL
IF ERRORLEVEL 1 GOTO Syntax
:: If the specified command contains a dot, it is not a macro nor an internal command
ECHO.%1 | FIND "." >NUL
IF NOT ERRORLEVEL 1 GOTO ExtCmd
:: Check if the command specified is a DOSKEY macro
FOR /F "tokens=1* delims==" %%A IN ('DOSKEY /MACROS 2^>NUL') DO (
IF /I "%~1"=="%%~A" (
ECHO.
ECHO -DOSKEY Macro-
GOTO:EOF
)
)
:: Next, check if the command specified is an internal command; to do so
:: reliably we need SysInternals' STRINGS; if it isn't available, we'll
:: just check against a list of known internal commands, risking "false
:: positives" in older CMD versions (e.g. MAKELINK does not exist in XP,
:: but without STRINGS.EXE this batch file will still return "-CMD
:: internal command-"; when STRINGS.EXE IS available, the batch file would
:: return "-None-")
FOR %%A IN (APPEND ASSOC BREAK CALL CD CHCP CHDIR CLS COLOR COPY DATE DEL DIR DPATH ECHO ENDLOCAL ERASE EXIT FOR FTYPE GOTO IF KEYS MD MKDIR MKLINK MOVE PATH PAUSE POPD PROMPT PUSHD RD REM REN RENAME RMDIR SET SETLOCAL SHIFT START TIME TITLE TRUENAME TYPE VER VERIFY VOL) DO (
IF /I "%~1"=="%%~A" (
STRINGS.EXE /? >NUL 2>&1
IF ERRORLEVEL 1 (
REM * * * NOT TESTED :: may return a false positive * * *
ECHO.
ECHO -CMD Internal Command-
GOTO:EOF
) ELSE (
REM * * * TESTED with STRINGS.EXE :: might still occasionally fail, though * * *
STRINGS.EXE "%ComSpec%" | FINDSTR.EXE /R /B /I /C:"%~1$" >NUL
IF NOT ERRORLEVEL 1 (
ECHO.
ECHO -CMD Internal Command-
GOTO:EOF
)
)
)
)
:ExtCmd
:: Search current directory first, then PATH, for the "pure"
:: file name itself or one of the extensions defined in PATHEXT.
:: Add quotes to match directory names with spaces as well.
SET Path="%CD%";"%!!%"
SET Found=-None-
:: This command line was partly rewritten by Yakov Azulay.
FOR %%A IN (%Path%) DO (
ECHO.%~1 | FIND "." >NUL
IF ERRORLEVEL 1 (
FOR %%B IN (%PathExt%) DO (
IF EXIST "%%~A.\%~1%%~B" (
CALL :Found "%%~A.\%~1%%~B"
)
)
) ELSE (
FOR %%B IN (.;%PathExt%) DO (
IF EXIST "%%~A.\%~1%%~B" (
CALL :Found "%%~A.\%~1%%~B"
)
)
)
)
:: Display the result
ECHO.
ECHO.%Found%
:: Done
GOTO End
:Found
:: Stop after finding the first match
IF NOT "%Found%"=="-None-" GOTO:EOF
:: Store the first match found
SET Found=%~f1
GOTO:EOF
:Syntax
ECHO.
ECHO WHICH, Version 5.20
ECHO UNIX-like WHICH utility for Windows 2000 and later
ECHO.
ECHO Usage: WHICH program_name
ECHO.
ECHO Notes: You may specify the program_name with or without extension, but
ECHO wildcards, drive, or path are NOT allowed.
ECHO This batch file first searches the list of DOSKEY macros, then
ECHO the list of CMD's internal commands, then the current directory,
ECHO and finally the PATH for the command specified.
ECHO If SysInternals' STRINGS is available and in the PATH, CMD's internal
ECHO commands will be verified.
ECHO If STRINGS is NOT available, internal commands are NOT verified, e.g.
ECHO even though MKLINK is not available in XP at all, the command
ECHO "WHICH MKLINK" would still return "-CMD internal command-" anyway.
ECHO.
ECHO Written by Yakov Azulay and Rob van der Woude
ECHO http://www.robvanderwoude.com
:End
IF "%OS%"=="Windows_NT" ENDLOCAL
page last modified: 2024-04-16; loaded in 0.0075 seconds