Do not execute the command to check its availability (that is, found in the PATH
environment variable). Use where
instead:
where 7z.exe >nul 2>nul IF NOT ERRORLEVEL 0 ( @echo 7z.exe not found in path. [do something about it] )
>nul
and 2>nul
prevent the display of the result of the where
command for the user. Direct execution of the program has the following problems:
- It’s not immediately clear what the program does
- Unintended side effects (file system changes, sending emails, etc.)
- Resource intensive, slow startup, I / O lock, ...
You can also define a procedure to help users verify that their system meets the requirements:
rem Ensures that the system has a specific program installed on the PATH. :check_requirement set "MISSING_REQUIREMENT=true" where %1 > NUL 2>&1 && set "MISSING_REQUIREMENT=false" IF "%MISSING_REQUIREMENT%"=="true" ( echo Download and install %2 from %3 set "MISSING_REQUIREMENTS=true" ) exit /b
Then use it like:
set "MISSING_REQUIREMENTS=false" CALL :check_requirement curl cURL https://curl.haxx.se/download.html CALL :check_requirement svn SlikSVN https://sliksvn.com/download/ CALL :check_requirement jq-win64 jq https://stedolan.imtqy.com/jq/download/ IF "%MISSING_REQUIREMENTS%"=="true" ( exit /b )
source share