Posted By

mwb1100 on 07/21/10


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

Tyster
abumap


which.cmd


 / Published in: DOS Batch
 

Windows cmd file to locate a file on the PATH.

  1. @echo off
  2. rem - search for the given file in the directories specified by the path, and display the first match
  3. rem
  4. rem The main ideas for this script were taken from Raymond Chen's blog:
  5. rem
  6. rem http://blogs.msdn.com/b/oldnewthing/archive/2005/01/20/357225.asp
  7. rem
  8. rem
  9. rem - it'll be nice to at some point extend this so it won't stop on the first match. That'll
  10. rem help diagnose situations with a conflict of some sort.
  11. rem
  12.  
  13.  
  14. rem - search the current directory as well as those in the path
  15. set PATHLIST=.;%PATH%
  16. set EXTLIST=%PATHEXT%
  17.  
  18. if not "%EXTLIST%" == "" goto :extlist_ok
  19. set EXTLIST=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
  20. :extlist_ok
  21.  
  22. rem - first look for the file as given (not adding extensions)
  23. for %%i in (%1) do if NOT "%%~$PATHLIST:i"=="" echo %%~$PATHLIST:i
  24.  
  25. rem - now look for the file adding extensions from the EXTLIST
  26. for %%e in (%EXTLIST%) do @for %%i in (%1%%e) do if NOT "%%~$PATHLIST:i"=="" echo %%~$PATHLIST:i

Report this snippet  

You need to login to post a comment.