2

I have two files: abc.exe and abc.cmd - both along the path somewhere. abc.exe is a command-line utility. abc.cmd is a wrapper around abc.exe.

When I type "abc" in the command prompt, how can I figure out which file is being run and the full path to it? I tried utils like where and which, but both require you to enter the extension to tell you where the file is being run from.

Neither stay up long enough for me to pull up process explorer and find out.

  • Is there some reason you can't just call the .cmd version when you need it? I know it's a whole extra 4 characters to specify, but if you're calling it from batch scripts or even by hand it seems like it's worth the effort if you really must know. – Laura Thomas Jun 18 '09 at 18:19
  • I've inherited an infrastructure that is used widely. People are used to invoking "abc" instead of "abc.cmd" or "abc.exe". –  Jun 18 '09 at 23:30
  • See http://technet.microsoft.com/en-us/library/cc723564.aspx, about half way down at a heading "Command Search Sequence" for (one version of) the official story. In short, . and PATH are searched in that order, checking each PATHEXT in each folder for a matching file. – RBerteig Jun 19 '09 at 00:02

7 Answers7

4

Have you tried Process Monitor? You could set the filter to "path contains 'abc'" and then sift through the results.

Ben Dunlap
  • 410
  • 5
  • 10
  • Beat me to it. My fav tool on the series of tubes. – MathewC Jun 18 '09 at 17:39
  • Looks like I chose the answer too soon. When I filter on abc, and run abc.cmd, there are no references to abc.cmd in ProcMon. I only see results relating to sd.exe (load image, openkey, etc). The process name does have cmd.exe, but I can't tell if it's the host process on which I'm executing commands on, or the one from abc.cmd. –  Jun 18 '09 at 18:00
  • I expect that you'll still be able to figure out those things with Process Monitor -- you'll just need to play with the filters a bit. – Ben Dunlap Jun 18 '09 at 18:44
  • ... keeping in mind that the filter doesn't prevent events from being captured, it just controls what you're seeing. – Ben Dunlap Jun 18 '09 at 18:44
  • ... so you can modify the filters as much as you want once you've captured data, and this will let you look at the data in different ways. And don't forget that there are lots of columns available that aren't displayed by default. Right-click on a column-label to see what's available. – Ben Dunlap Jun 18 '09 at 18:46
1

Just put an ECHO command in the cmd file and you'll know quickly enough. Might not be what you want for long term, but it'll do for this purpose.

Maximus Minimus
  • 8,987
  • 2
  • 23
  • 36
  • Thanks mh. While this helps, I am looking for a long-term solution. I would bump up this solution if I can, but I don't have enough cred. –  Jun 18 '09 at 18:09
  • Selecting this because this is the short-term answer. The real long-term answer that- it looks like the system just search in the path in order, looking for files that match PATHEXT with the precedence specified in PATHEXT. There isn't a tool that seems to fish this out easily, needs manual checking and comparing. Not terribily difficult since 'where' returns a list of files from the path. –  Jun 18 '09 at 23:33
1

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.

Helvick
  • 20,019
  • 4
  • 38
  • 55
  • The search order implemented in this perl script matches the documented order found at http://technet.microsoft.com/en-us/library/cc723564.aspx exactly. The current directory is searched first, followed by each directory in PATH. In each directory, extensions are tried from PATHEXT in the order listed in PATHEXT. It is typical of MS to only barely document the detailed behavior of CMD.EXE. – RBerteig Jun 19 '09 at 00:01
0

You can try doing cmd /K abc and it will run but will leave the command prompt open. Hope this helps.

Hondalex
  • 693
  • 4
  • 8
0

From earlier memories (refreshed at Microsoft KB35284) the execution order is going to be EXE and then CMD (or BAT in earlier days). So, if you skip the extension and both are in local folder, the EXE will take precedence and execute.

The interesting part is, if you used the tab-expand the CMD (or BAT) will be expanded too (coz, that uses alphabetical ordering).

If you get interested in the CMD and BAT file differences lookup windows-batch-files-bat-vs-cmd

nik
  • 7,100
  • 2
  • 25
  • 30
  • Thanks. In my case, abc.cmd and abc.exe are in different folders. As it turns out the cmd file is being executed instead of the exe. I'm guessing it has to do with the order in the path variable. –  Jun 18 '09 at 18:08
  • @Gokul, you are right about that. – nik Jun 19 '09 at 01:24
0

Here is a little tool I wrote that will tell you where the first occurance of a file is in your path. To findout exactly what is running on your system use ProcessExplorer from sysinternals.

example: where.bat

@echo off
setlocal

if "%1"=="" goto USAGE
set filename=%1

for %%i in (%filename%) do @echo %%~dp$PATH:i%%i

goto EOF

:USAGE

echo %0 filename

:EOF
endlocal
Jeremy E
  • 197
  • 1
  • 5
  • This searches for the first occurance of a specific file. I want to know how an extension-less command is being resolved. In essence, I want to figure out which file is executed as well as the path. Your solution just figures out the path given the filename. –  Jun 18 '09 at 18:06
0

In Windows Vista there is a program called where.exe. You can use it as where abc. It will show the full path to abc.

  • 'where' requires the file extension since it search for exact matches. Adding wildcards just returns both. –  Jun 18 '09 at 18:02