Based on both Mick's answer and Phoshi's I came up with the solution I was looking for. I created an AutoIt script that loops through all the PATHEXT's and PATH's to find the first location where the passed parameter is located at. It handles both extension-less as well as extension-full parameters. It first searches the local working directory, then the environment variables. As far as everything I've read, this is how Windows performs it's lookup for the command as well. Here's the script:
If $CmdLine[0] > 0 Then
$commandToFind = $CmdLine[1]
$matchFound = false
$foundPath = ""
$pathEnvironmentVariable = EnvGet("PATH")
$pathDirectories = StringSplit($pathEnvironmentVariable, ";", 2)
$pathExtensionsEnvironmentVariable = EnvGet("PATHEXT")
$pathExtensions = StringSplit($pathExtensionsEnvironmentVariable, ";", 2)
; Search the local directory first for the file
If FileExists($commandToFind) Then
$matchFound = true
$foundPath = @WorkingDir & "\" & $commandToFind
EndIf
For $pathExtension In $pathExtensions
$fullPath = @WorkingDir & "\" & $commandToFind & StringLower($pathExtension)
If FileExists($fullPath) Then
$matchFound = true
$foundPath = $fullPath
ExitLoop
EndIF
Next
If Not $matchFound == true Then
; Loop through all the individual directories located in the PATH environment variable
For $pathDirectory In $pathDirectories
If FileExists($pathDirectory) Then
$pathWithoutExtension = $pathDirectory & "\" & $commandToFind
; Check if the command exists in the current path. Most likely the parameter had the extension passed in
If FileExists($pathWithoutExtension) Then
$matchFound = true
$foundPath = $pathWithoutExtension
ExitLoop
EndIf
If Not $matchFound == true Then
; Loop through all possible system extensions to see if the file exists.
For $pathExtension In $pathExtensions
$fullPath = $pathWithoutExtension & StringLower($pathExtension)
If FileExists($fullPath) Then
$matchFound = true
$foundPath = $fullPath
ExitLoop
EndIf
Next
EndIf
If $matchFound == true Then
ExitLoop
EndIf
EndIf
Next
EndIf
If $matchFound == true Then
ConsoleWrite("Located at " & $foundPath & @CRLF)
Else
ConsoleWriteError("Unable to locate the command" & @CRLF)
EndIf
EndIf
To get it to work, you need to run it through the Aut2exe.exe program that comes with AutoIt (shortcut is titled Compile Script to .exe
) and check the "Console?" checkbox when compiling it. I just compile it to a file called "which.exe" that I toss in an existing path directory (like C:\Windows\System32
).
If I understand correctly, you're looking for something similar to the GNU "which" command. – Moshe – 2009-11-30T22:28:49.150