19 March 2008

Poor Man's Which Command For Windows Cmd.exe

The Windows Command Processor (cmd.exe) doesn't seem to have an equivalent to a Unix shell's which command to find the directory in the PATH variable of a given executable. Here's a poor man's version using the for command:

>for /F %i in ("notepad.exe") do @echo %~$PATH:i
C:\WINDOWS\system32\notepad.exe
>for /F %i in ("gawk.exe") do @echo %~$PATH:i
C:\Program Files\GnuWin32\bin\gawk.exe

20-Mar-2008: Added as a macro using doskey:

>doskey which=for %i in ("$1") do @echo %~$PATH:i