Perhaps How do I get the application exit code from a Windows command line? and Redirect Windows cmd stdout and stderr to a single file might help you.
Run
perl -e1 2>NUL
if errorlevel 1 (
echo Perl is not installed
)
perl -e1
simply executes the Perl expression 1
as a one-liner which always is successful if Perl is installed. It produces no ouput at all, except it complains when Perl isn't found. That's why I redirected STDERR to NUL so you will not see any output, even not the error messages.
The if errorlevel 1
checks whether the returncode of the last command (perl -e1
in this case) was >=1
. If Perl is installed and was executable then its returncode will be 0
(meaning success) and the if
won't trigger.
You could also use perl -v
but that produces output on STDOUT. In that case you would have to redirect both STDOUT and STDERR to NUL, like so: perl -v >NUL 2>&1
.