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:
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!