- 1 year ago
Check a program existence in PATH
To check if a program exists in the PATH
environment variable in Windows using a batch script, you can use the where
command. The where
command searches for executable files in the directories listed in the PATH
and returns the paths of the matching files.
Here's a simple batch script that checks if a program exists in the PATH
:
batch@echo off
set "program_name=your_program.exe"
where "%program_name%" > nul 2>&1
if %errorlevel% equ 0 (
echo Program exists in PATH.
) else (
echo Program does not exist in PATH.
)
Replace your_program.exe
with the name of the program you want to check. The where
command will search for the specified program in the PATH
, and the %errorlevel%
variable will be set to 0
if the program is found. If the program is not found, %errorlevel%
will be non-zero.
The > nul 2>&1
at the end of the where
command is used to suppress the output and error messages from the where
command, as we are only interested in the %errorlevel%
value.
Note that if the program is found in the PATH
, the batch script will print "Program exists in PATH." If the program is not found or if there is an error in the script, it will print "Program does not exist in PATH."