'which' is a Unix command used to identify the location of executables by searching in the directories listed in the PATH environment variable.

There are some 3rd party tools that implement the ‘which’ functionality in Windows, but do you know that the recent versions of Windows actually come with a tool called 'where' that is able to accomplish the same task as the 'which' utility ? Now, let's see how we can use that utility to determine the location of the 'java.exe' executable.

d:\util>where java
C:\Windows\System32\java.exe

A couple of years back, I also wrote a simple batch file that actually implements the same functionality of 'which'. Here's the code:

@echo off

:: check for illegal charecters
if "%1"=="" goto USAGE
echo %1 | find /v ":" | find /v "\" | find /v "*" | find /v "?" | find /v "," | find /v ";" | find /v "/"  | find "%1" > nul
if errorlevel 1 goto USAGE

:: actual 'which' logic
for %%a in (.;%pathext%) do for %%b in (%1%%a) do ( echo %%~f$PATH:b | find /i "%1" )
goto END

:USAGE
:: Help screen:

echo.
echo UNIX-like which utility for Windows
echo Written by Ibrahim - www.digitalinternals.com
echo.
echo Usage:  which  executable_name
echo.
echo you may specify executable_name with or without
echo extension, but without a drive, path,
echo spaces or wildcards character(s).
echo.

:END

Copy the above code to a batch file called "which.bat" and place it somewhere in your machine's PATH.

Ibrahim is a Software Engineer, Blogger and Mobile Telecom Network professional. A technology enthusiast with great interest in open source projects.