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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With