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

WindowsBatch FileBatch Processing

Windows Problem Overview


I'm looking for a simple way to test if an executable exists in the PATH environment variable from a Windows batch file.

Usage of external tools not provided by the OS is not allowed. The minimal Windows version required is Windows XP.

Windows Solutions


Solution 1 - Windows

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 notepad
C:\Windows\System32\notepad.exe
C:\Windows\notepad.exe

D:\>where where
C:\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 myapplication
IF 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

Solution 2 - Windows

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.

Solution 3 - Windows

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

file.exe /?  2> NUL
IF 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).

Solution 4 - Windows

This can be accomplished via parameter substitution.

%~$PATH:1

This returns the full path of the executable filename in %1, else an empty string.

This does not work with user-defined variables. So if the executable filename is not a parameter to your script, then you need a subroutine. For example:

call :s_which app.exe
if not "%_path%" == "" (
  "%_path%"
)

goto :eof

:s_which
  setlocal
  endlocal & set _path=%~$PATH:1
  goto :eof

See http://ss64.com/nt/syntax-args.html

Solution 5 - Windows

For those looking for a PowerShell option. You can use the Get-Command cmdlet passing two items. First give the current dir location with .\ prefixed, then give just the exe name.

(Get-Command ".\notepad", "notepad" -ErrorAction Ignore -CommandType Application) -ne $null

That will return true if found local or in system wide paths.

Solution 6 - Windows

@echo off
set found=
set prog=cmd.exe
for %%i in (%path%) do if exist %%i\%prog% set found=%%i
echo "%found%"
if "%found%"=="" ....

Solution 7 - Windows

Sometimes this simple solution works, where you check to see if the output matches what you expect. The first line runs the command and grabs the last line of standard output.

FOR /F "tokens=*" %%i in (' "xcopy /? 2> nul" ') do SET xcopyoutput=%%i
if "%xcopyoutput%"=="" echo xcopy not in path.

Solution 8 - Windows

Use command : powershell Test-Path "exe which you looking for"

It will return True if its present, otherwise False.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsorinView Question on Stackoverflow
Solution 1 - WindowsRyan BemroseView Answer on Stackoverflow
Solution 2 - WindowsJoeyView Answer on Stackoverflow
Solution 3 - WindowseadmasterView Answer on Stackoverflow
Solution 4 - WindowsChris NoeView Answer on Stackoverflow
Solution 5 - WindowsJohn CView Answer on Stackoverflow
Solution 6 - WindowsPabloGView Answer on Stackoverflow
Solution 7 - WindowsbdombroView Answer on Stackoverflow
Solution 8 - WindowsakkidukesView Answer on Stackoverflow