Windows Batch which(1)

Ever wondered which exact executable will be executed when running a command from the command line? UNIXes and Linux have which(1) which tells that. There are implementations on Windows, but not one in batch language I was aware of :-)

So that naturally called for ugly things to be done. I wrote this a while ago and noticed that it does not always works correctly on Windows Vista. At the time of its writing I was working with Windows 2000 and it worked pretty well there. Somehow something is messing with extensions:
C:\>which which
C:\Users\Johannes Rössel\SVN\Pr­ojekte\CMD\which­\which.cmd
obviously works.
C:\>which which.cmd
doesn't. Contrary to that
C:\>which wget
does not work, but
C:\>which wget.exe
C:\Users\Johannes Rössel\Apps\wget­.exe
does. For things in the system's own paths (which are searched first) the above problems do not seem to apply:
C:\>which explorer
C:\Windows\ex­plorer.exe

C:\>which explorer.exe
C:\Windows\ex­plorer.exe
Weird, indeed. But currently I lack time to investigate further. I know it worked pretty well once, maybe I look into it again some day. Until then it's just another random example of Windows Command Processor perversion :-)

UPDATE (2008–06–01): I found another bug that manifests itself most prominently on Windows Vista x64, concerning paths with closing parentheses in them (as happens when installing x86 applications there). That means I have to do a bit escaping as soon as those paths show up in the argument of a FOR loop. Using ! and FOR /F seems to work somehow, except that I only get a single token from that.

AttachmentSize
which.cmd1.98 KB

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.