I have more little command line utilities than I can ever seem to keep track of. Worse still, sometimes, I have no idea where the one that executes when I type its name at the command prompt actually lives! That became so frustrating, I wrote this quick little utility to search the entire command path to find the first, or all, file(s) that match so you can quickly find the one you just can't put your finger on.

Were this simply a tool to find the executable that runs based on the search path, it would actually be a simple matter of passing its name to the FindExecutable API:

Color-coded with vbMarkUp - try it today!
Private Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" ( _
   ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long

Private Function FindApplication(ByVal DocName As String) As String
   Dim Result As String
   Dim n As Long
   Const MAX_PATH As Long = 260
   ' Find executable associated with this document.
   Result = Space$(MAX_PATH)
   n = FindExecutable(DocName, vbNullString, Result)
   Debug.Print n
   If n > 32 Then
      FindApplication = _
         Left$(Result, InStr(Result, vbNullChar) - 1)
   End If
End Function

Yeah, honest. FindExecutable will return, for example, "C:\WINDOWS\system32\notepad.exe" if you just pass it "notepad". I don't think that's very widely known. It will, of course, also return the name of whatever exectuble is associated with any existing document file.

But FindExecutable doesn't do the entire job, if you want to find all instances of a given executable on the search path. (You never install/copy different versions of a file to different locations?) So there's need to get a bit loopy, iterating the PATH for all possible matches, looking at each combination of PATHEXT entries.

This isn't very mind-blowing code, here, folks. Just a tool to do what we all get tired of doing over and over and over manually. And that, really, is what it's all about, right? Enjoy!


This sample, or the one from which it originally derived, was published (or at least peripherally mentioned) in the following article(s):

APIs Usage

This sample uses the following API calls:

Module Library Function
MFindExe.bas kernel32


Don't see what you're looking for? Here's a complete API cross-reference.


Download Which.zip   Please, enjoy and learn from this sample. Include its code within your own projects, if you wish. But, in order to insure only the most recent code is available to all, I ask that you don't share the sample by any form of mass distribution.

Download Which.zip, 44Kb, Last Updated: Thursday, September 24, 2009

See Also

The following resources may also be of interest: