4

If I have a long and involved PATH and I type the name of some command, how do I find out where the EXE or BAT file actually lives. I thought TRUENAME did this but it's gone from XP and I'm not sure it did exactly what I want.

So I want to be able to type

WHEREIS "Obscure Tool.exe"

and I get:

C:\Program Files\Obscure Tools Limited\Obscure Tool\Obscure Tool.exe

when the folder C:\Program Files\Obscure Tools Limited\Obscure Tool\ is on my path. What is WHEREIS called, or can it be done with a batch file?

2

6 Answers 6

1

Here is a windows batch file that will search your path for a file. I have tested it on XP and Win7.

Please understand that a batch/command file is NOT a good solution to this problem because string manipulation is painfully slow - but in lieu of a compiled tool, this will do what you desire.

Hopefully it is commented well enough, but feel free to ask questions if anything needs clarification.

@echo off

SETLOCAL ENABLEDELAYEDEXPANSION

rem -- SRC = Search value. Eg. notepad  or  notepad.exe
rem -- PAT = A copy of the path
rem -- SEP = PATH seperator
rem -- EXE = Executable file extensions
SET TMP_SRC=%1
SET TMP_PAT=%PATH%
SET TMP_SEP=;
SET TMP_EXE=exe com bat cmd

rem -- This is a batch file ...they are s-l-o-w
rem -- "<nul set /p=" does "echo" without a newline
<nul set /p =Searching...

:strLoop
    rem -- SUB = The first entry in the current PATH
    for /f "delims=%TMP_SEP%" %%s in ("!TMP_PAT!") do set TMP_SUB=%%s

    rem -- Progress update
    <nul set /p =.

    rem -- Check for the file exactly as specified on the command line
    if exist "!TMP_SUB!\%TMP_SRC%" (
        echo.
        echo Found "!TMP_SUB!\%TMP_SRC%"
        goto strDone
    )

    rem -- Check for the file with each of the selected executable extensions
    for %%x in (%TMP_EXE%) do (
        if exist "!TMP_SUB!\%TMP_SRC%.%%x" (
            echo.
            echo Found "!TMP_SUB!\%TMP_SRC%.%%x"
            goto strDone
        )
    )

    rem -- This loop chops off the first entry of the PATH
:subLoop
    rem -- CHR = First character of current PATH
    rem -- PAT -> All but the first character of the current PATH
    set TMP_CHR=!TMP_PAT:~0,1!
    set TMP_PAT=!TMP_PAT:~1!

    rem -- Have we exhausted the PATH?
    if "!TMP_PAT!" EQU "" goto strDone

    rem -- Have we just removed a PATH seperator?
    if "!TMP_CHR!" EQU "%TMP_SEP%" goto strLoop

    rem -- Keep stripping characters
    goto subLoop
:strDone

rem -- Cleanup
SET TMP_SRC=
SET TMP_PAT=
SET TMP_SEP=
SET TMP_SUB=
SET TMP_CHR=
SET TMP_EXE=

ENDLOCAL
1
  • +1 for effort (and for the tip about echoing text without a CR). I haven't looked it over to see how it works, but I have tried it and it only takes a few seconds on my machine.
    – rossmcm
    Oct 5, 2014 at 12:19
5

It all depends from what command interpreter one is running, and there isn't an external utility that is going to do this properly in all cases. This is for a very imporant reason:

Different command interpreters look for commands in different ways.
Only a command built in to each individual command interpreter will truly know what will be invoked by a particular command name.

  • For JP Software's TCC, simply use its built-in which command. This understands TCC's aliases, executable extensions, and path search algorithm.
    [c:\]which mspaint dir
    mspaint is an external : C:\Windows\system32\mspaint.exe
    dir is an internal command
    [c:\]
  • For the Korn shell, ksh, as available in the SFUA utility toolkit (which run in the Subsystem for UNIX-based Applications that comes right there in the box with Windows 7 Ultimate edition and Windows Server 2008 R2) use its built-in whence command. This understands the Korn shell's aliases, functions, built-in commands, and so forth.
    $ whence -v ls
    ls is a tracked alias for /bin/ls
    $
  • For the Bourne-Again shell, bash, as available for Services for Unix Applications as well as in Cygwin, use its built-in type command. This understands the Boune-Again shell's aliases, functions, built-in commands, and so forth.
    $ type '['
    [ is a shell builtin
    $
  • For Microsoft's command interpreter, cmd, you're out of luck. There's no built-in command for doing this; and although one can employ external commands that will replicate the path search algorithm, those commands will not necessarily (or even usually) recognize aliases and built-in commands. For examples: Here is the GnuWin32 which command failing to recognize dir (in contrast with TCC's built-in which command above):
    C:\>which dir
    which: no dir in (.;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem)
    C:\>
    And here is the where command mentioned by grawity below failing in exactly the same fashion:
    C:\>where dir
    INFO: Could not find files for the given pattern(s).
    C:\>
2
  • 3
    Recent Windows versions come with a tool named where (which is still an external command, though). Sep 8, 2011 at 21:28
  • I wouldn't say "where" is failing; its spec is to search the current directory then the PATH for files matching the search pattern, not commands. It should be available from Vista and above.
    – RJFalconer
    Feb 12, 2013 at 10:37
4

If you have GnuWin32 it's as simple as:

which ObscureTool.exe

Even better, if you're invoking it by name only (without an extension, as in "ObscureTool"), it'll still find the right thing. This might be an issue if you have two "executable" files: "ObscureTool.exe" and "ObscureTool.bat". In this case, WHICH will find the one that occurs first in the path. In this case, you'd merely say"

which ObscureTool
0
4

I am running regular Windows 7 and simply typing "where java.exe" seems to work great without any additional software.

3

Using powershell you can use the Get-Command tool.exe

2

You could use 'dir', the directory listing tool:

dir "c:\Obscure Tool.exe" /s/b

The /s means search subdirectories, and since you are starting in the root folder (C:) it will search all the folders on that drive.
The /b makes it display the result in "bare" format with no heading information or summary (ie. you will just get the path and filename).

4
  • 1
    +1 This is my favorite brute-force search method, if Windows' GUI search tools fail me for whatever reason (though since Vista, they've gotten better, in my opinion). Aug 23, 2011 at 0:31
  • 3
    this will list ALL files called Obscure Tool. Not the one in the path.
    – Keltari
    Aug 23, 2011 at 0:32
  • 1
    Everything.exe is always faster than a normal search through lots of directories. It does it immediately. Though probably no command line option like output on command line.
    – barlop
    Sep 3, 2011 at 21:06
  • And it won't find files that are in the path but on another drive.
    – DavidPostill
    Oct 3, 2014 at 15:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .