If two separate PATH directories contain a same-named executable, how does Windows choose?

2

I'm in the process of upgrading PEAR (PHP) on my system. The upgrade script is encouraging me to add "..\PHP\PEAR" to my PATH so that I can use "pear.bat". However, I already am able to use pear.bat.

Looking in my PATH, I see that I don't have any PEAR directories, only my PHP directory. Opening my PHP directory, I see that there's a "pear.bat" in the base. But there's also a pear.bat in the PEAR subfolder of PHP. I'm wondering if I borked a PEAR install.

I digress. So if I leave ..\PHP in my path, but also add ..\PHP\PEAR -- both of which have a "pear.bat" in them -- which one will Windows "choose"? How does Windows decide?

Coldblackice

Posted 2012-11-16T08:37:41.150

Reputation: 4 774

You could just try it and see. I would expect that since PATH is an ordered list, the first one it found would be used. – martineau – 2012-11-16T08:48:28.273

Answers

3

First match wins. The pear.bat in the directory that comes first in the %PATH% is the one that is executed:

  • PATH=..\PHP\PEAR;..\PHP: ..\PHP\PEAR\pear.bat is executed
  • PATH=..\PHP;..\PHP\PEAR: ..\PHP\pear.bat is executed

Ansgar Wiechers

Posted 2012-11-16T08:37:41.150

Reputation: 4 860

3

From http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/path.mspx?mfr=true:

Two or more identical file names in the path

If you have two or more files in the command path that have the same file name and extension, Windows XP searches for the specified file name first in the current directory, and then it searches the directories in the command path in the order in which they are listed in PATH.

Agreed, this applies to Windows XP (I can't find any information about other Windows versions), but I don't think this behavior has changed.

EDIT: I could find some information for Windows Server 2008 and Vista here: http://technet.microsoft.com/en-us/library/cc753427%28v=ws.10%29.aspx (almost same text as above):

If two or more files in the command path have the same file name and extension, path first searches for the specified file name in the current directory. Then it searches the directories in the command path in the order that they are listed in the PATH environment variable.

which seems to confirm the assumption that the behavior hasn't changed across Windows versions.

jaume

Posted 2012-11-16T08:37:41.150

Reputation: 4 947