2

I would like to write a batch file to silently install Perl MSI. However, the server/PC may have Perl installed, the batch file's flow would be:

  1. Check if Perl is installed.
  2. If not installed, install it silently.

I know that the command perl-v reports Perl version if Perl is installed, but do not have idea how to check whether the perl command is executable on the server/PC by windows batch file.

ermai
  • 51
  • 1
  • 5
  • 1
    http://stackoverflow.com/questions/4781772/how-to-test-if-an-executable-exists-in-the-path-from-a-windows-batch-file – stevieb Jun 19 '16 at 14:28
  • 1
    *"If not installed, install it silently"* I wouldn't be so sure about doing that, especially without a `Do you want to install Perl?` prompt. There are several situations where an installation may not be appropriate, and it may even be that there is already a copy installed, or present on the PATH with a different file name like `perl.5.24.0.exe`. You should leave the user in control of their own PC. – Borodin Jun 19 '16 at 15:16
  • Installing programs without the user's consent is not good citizenship. Viruses do that. Don't be a virus. – lit Jun 20 '16 at 18:04

2 Answers2

3

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.

Community
  • 1
  • 1
PerlDuck
  • 5,610
  • 3
  • 20
  • 39
  • 2
    This works because _"If a non-existent command is entered for execution, set ERRORLEVEL = 9009"_. Further details [here](http://stackoverflow.com/questions/34987885/what-are-the-errorlevel-values-set-by-internal-cmd-exe-commands/34987886#34987886) – Aacini Jun 19 '16 at 14:22
0
>nul 2>nul where perl || echo not installed

this checks for perl without trying to run perl.

where prints to STDOUT if it finds the file/command in folders listed in %PATH% or prints to STDERR if it doesn't.

NanoPi
  • 1
  • 1