How to test if an executable exists in the %PATH% from a windows batch file? How to test if an executable exists in the %PATH% from a windows batch file? windows windows

How to test if an executable exists in the %PATH% from a windows batch file?


Windows Vista and later versions ship with a program called where.exe that searches for programs in the path. It works like this:

D:\>where notepadC:\Windows\System32\notepad.exeC:\Windows\notepad.exeD:\>where whereC:\Windows\System32\where.exe

For use in a batch file you can use the /q switch, which just sets ERRORLEVEL and doesn't produce any output.

where /q myapplicationIF ERRORLEVEL 1 (    ECHO The application is missing. Ensure it is installed and placed in your PATH.    EXIT /B) ELSE (    ECHO Application exists. Let's go!)

Or a simple (but less readable) shorthand version that prints the message and exits your app:

where /q myapplication || ECHO Cound not find app. && EXIT /B


for %%X in (myExecutable.exe) do (set FOUND=%%~$PATH:X)if defined FOUND ...

If you need this for different extensions, just iterate over PATHEXT:

set FOUND=for %%e in (%PATHEXT%) do (  for %%X in (myExecutable%%e) do (    if not defined FOUND (      set FOUND=%%~$PATH:X    )  ))

Could be that where also exists already on legacy Windows versions, but I don't have access to one, so I cannot tell. On my machine the following also works:

where myExecutable

and returns with a non-zero exit code if it couldn't be found. In a batch you probably also want to redirect output to NUL, though.

Keep in mind

Parsing in batch (.bat) files and on the command line differs (because batch files have %0%9), so you have to double the % there. On the command line this isn't necessary, so for variables are just %X.


Here is a simple solution that attempts to run the application and handles any error afterwards.

file.exe /?  2> NULIF NOT %ERRORLEVEL%==9009 ECHO file.exe exists in path

Error code 9009 usually means file not found.

The only downside is that file.exe is actually executed if found (which in some cases is not desiderable).