UNIX-like ‘which’ Utility for Windows

'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

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

:: Help screen:

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


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

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, perl, php, python, java, javascript, nodejs, angular, react); plays_on(xbox, ps4); linux_desktop_user(true); }