Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 959
sorin Avatar asked Jan 24 '11 12:01

sorin


People also ask

How do you verify if a file exists in a batch file?

Checking for the existence of a file can be accomplished by using IF EXIST in a batch file called from the login script, or by using the login script ERRORLEVEL variable with a MAP statement. The COMMAND.COM /C will close the CMD box window automatically after it terminates.

How do I run an EXE from a batch file?

To start an exe file from a batch file in Windows, you can use the start command. For example, the following command would start Notepad in most versions of Windows. The start command can be used for other exe files by replacing the file path with the path to the exe file.


Video Answer


1 Answers

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 
like image 139
Ryan Bemrose Avatar answered Sep 20 '22 16:09

Ryan Bemrose