@ECHO OFF
REM  which
REM
REM  NAME
REM
REM    which - display the actual command to be executed
REM
REM  SYNOPSIS
REM
REM    which command
REM
REM  DESCRIPTION
REM
REM    The which(1) command displays the command that will be executed by the
REM    shell after substitutions, path searching, and so on.
REM
REM
REM This batch accomodates for
REM 
REM "If the file name does not contain a directory path, the system searches for
REM the executable file in the following sequence:
REM 
REM 1. The directory from which the application loaded.
REM 2. The current directory for the parent process.
REM 3. Windows 95/98/Me: The Windows system directory. [...]
REM    Windows NT/2000/XP: The 32-bit Windows system directory. [...]
REM    Windows NT/2000/XP: The 16-bit Windows system directory.
REM      There is no function that obtains the path of this directory,
REM      but it is searched. The name of this directory is System.
REM 4. The Windows directory. Use the GetWindowsDirectory function [...]
REM 5. The directories that are listed in the PATH environment variable."
REM 
REM           MSDN Library, CreateProcess
REM
REM Also it accomodates for a file with extension already given
REM
SETLOCAL ENABLEEXTENSIONS
REM Cache first parameter -- the file to look for
SET filename=%1
REM Construct search path, we need some special treatment in the beginning for
REM a few folders that have higher search priority and do not necessarily need
REM to appear in %PATH%
SET searchpath=.;%SystemRoot%\system32;%SystemRoot%\System;%SystemRoot%;%PATH%
REM First assume there is an extension given
FOR %%i IN (%filename%) DO SET result=%%~$PATH:i
REM Main search loop for extensions and paths
FOR %%p IN (%searchpath%) DO (
  FOR %%e IN (%PATHEXT%) DO (
    IF DEFINED result (
      GOTO end
    ) ELSE (
      SET temppath=%%p
      FOR %%i IN (%filename%%%e) DO (
        SET result=%%~$temppath:i
      )
    )
  )
)
:end
IF DEFINED result ECHO %result%
ENDLOCAL
goto :EOF
