Thanks Dave for sharing this useful information!
I've updated my which program to test for this when displaying the pathname of the program that will run.
It now skips the instance in the current directory (if any) if NoDefaultCurrentDirectoryInExePath is defined.
After a bit of hesitation, I decided to keep displaying the instance in the current directory anyway when using the which -a option.
The rationale being that when using which -a, you want to enumerate all alternative versions that could be run.
Code: Select all
C:\JFL\Temp>which wm
wm.bat
C:\JFL\Temp>set NoDefaultCurrentDirectoryInExePath=1
C:\JFL\Temp>which wm
C:\JFL\Tools\wm.bat
C:\JFL\Temp>which -a wm
wm.bat
C:\JFL\Tools\wm.bat
C:\JFL\Temp>
The source is available there:
which.cA build is available there:
which.exeEventually it'll be in the next release of my tools library:
SysTools.zip(Which (no pun intended
) unfortunately I last released this morning, before knowing about all this!)
PS. I also made a quick test in PowerShell. Whether NoDefaultCurrentDirectoryInExePath is defined of not, the current directory is never searched, even when running batch files.
So the output of my which.exe did not need to change when invoked within PowerShell. It already skipped the current directory in all cases.