CMD Batch files are pretty weak at this but if you don't mind drifting into Perl.
$filename=shift() or die ("Enter a filename\n");
@extensions = split(/\;/,$ENV{PATHEXT});
@paths = (".",split(/\;/,$ENV{PATH}));
foreach $path (@paths) {
$path =~ s/\\?\s*$/\\/;
foreach $ext (@extensions) {
if (-e $path.$filename.$ext) {
print $path.$filename.$ext."\n";
}
}
}
Given an extensionless filename it will list the order in which the various executable file types (as defined by the PATHEXT variable) will be found starting with the current directory and then searching the PATH sequentially for all executable types in the correct order. This search pattern is the one used by the CMD shell as far as I am aware, for commands launched by api calls such as CreateProcess the search behavior and order will be different.
This is quick and dirty and I haven't exhaustively tested it but it handles paths with spaces and paths with\without a trailing backslash which are the most obvious complications. Odd paths with forward slashes and quoted semicolons will throw it for a loop.