45

If I have a.com, a.cmd, a.bat, and a.exe files in my %PATH%, which one would Windows pick if I invoke just the command a? Is this officially spec-ed somewhere by Microsoft?

I just wanted to wrap my gvim.exe executable with -n, but my gvim.bat doesn't appear to get run neither from the command line, nor from the Run dialog.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Jeenu
  • 2,109
  • 2
  • 20
  • 27

3 Answers3

32

See the command search sequence on Microsoft Docs

The PATH and PATHEXT environmental variables each provide an element of the search sequence: PATH is the ordered list of directories "where" to look, and PATHEXT is the ordered list of file extensions ("what") to look for (in case the extension isn't explicitly provided on the command line). Prior to using the PATH however, the current directory is searched.

The PATHEXT variable defaults to ".COM;.EXE;.BAT;.CMD"

kapex
  • 28,903
  • 6
  • 107
  • 121
mjv
  • 73,152
  • 14
  • 113
  • 156
  • I just invoke gvim from Run dialog, and no matter what the PATHEXT is set to, it's only gvim.exe that runs; even if I say gvim.bat at the run dialog. – Jeenu Oct 31 '09 at 05:29
  • 2
    @Jeenu, this is rather odd. A likely explanation however is that another instance of gvim (or a bat file referencing it) is found before the PATH location where you expect it. Review the path manually, just like the console would do. (or another way to prove this is to temporarily rename the exe in the directory where you think it comes from see whether invoking the command doesn't fail by not finding the exe. – mjv Oct 31 '09 at 06:12
  • Thanks for the *"in case the extension isn't explicitly provided on the command line"* note, that's why `python3.5` command cannot work with PATH on windows... – dedek Jun 28 '16 at 15:00
21

Summarized from the Microsoft Technet link provided by mjv:

Windows will step through the directories in PATH from left to right (normal list order). In each of these, it attempts to locate a file with each PATHEXT also left to right, before continuing to the next PATH entry. The first file it finds wins.

So, in your case, you need to change PATHEXT so that .BAT comes before .EXE (or it will always find .exe first and not the .bat you most likely want to override it, if in the same directory) and also put the path of your 'override' gvim.bat earlier (earliest?) in the PATH listing than anywhere it could find gvim.* (because it tries every PATHEXT in each before going on down PATH)

Tony Butler
  • 356
  • 2
  • 7
3

I believe it walks through %PATHEXT% and tries each of those

Jim Deville
  • 10,632
  • 1
  • 37
  • 47