Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is there a way to quickly determine where a binary (that the %PATH% variable points to) is located.

That is something that would do the equivalent of which perl in a traditional unix shell.

I understand that The resource Kit has a where command, but this kit is not installed on my (corporate)-computer, so I cannot use where.

share|improve this question

2 Answers 2

up vote 5 down vote accepted

A cmd.exe one-liner for which would be:

for %G in (<binary.exe>) do @echo.%~$PATH:G

You can code it as a simple which.bat:

@echo off
for %%G in (%1) do @echo.%%~$PATH:G

EDIT: it requires full name of binary: which perl.exe, not which perl. It is possible to write a batch file that takes %PATHEXT% into consideration, so it does not need the full name of binary. Please let me know if you need it.

EDIT2: anyway I decided to write the batch file. Here it is:

@echo off
setlocal enabledelayedexpansion

set ext= ;%PATHEXT%
:extloop1
for /f "delims=; tokens=1,*" %%A in ("!ext!") do (
  if exist %1%%A (
    echo .\%1%%A
    goto finish
  )
  set ext=%%B
)
if "!ext!" neq "" goto extloop1

set ext= ;%PATHEXT%
:extloop2
for /f "delims=; tokens=1,*" %%A in ("!ext!") do (
  for %%C in (%1%%A) do (
    if exist %%~$PATH:C (
      echo %%~$PATH:C
      goto finish
    )
  )
  set ext=%%B
)
if "!ext!" neq "" goto extloop2

:finish
endlocal

First, as per Bob's sugestion, it checks if the binary exists in the current directory. If the binary is not found, another loop is executed which does the search through %PATH% variable.

Loops are based on if ... goto, for command is used only to split the ext value by ';' character. The value of %PATHEXT% is prepended with an empty extension, which allows for searching for fully named binary - both which perl and which perl.exe works.

share|improve this answer
1  
Well, if you do end up writing an extended batch file, might as well go all the way and check .\binary.* first. The operating system always searches in the current directory first, before it searches the directories in the command path. –  Bob Apr 16 '12 at 10:06
    
@Bob: Good point, thanks! –  MBu Apr 16 '12 at 10:22

You could try Paul Sadowski's collection of utilities - he includes a which. See it at: http://www.paulsadowski.com/wsh/cmdprogs.htm

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.