Search code examples
batch-filewindows-installer

How to check existence of an executable and run an installer if application is not installed?


I am a system administrator of a server/client environment that doesn't have SCCM and I want to check if computers in my organization have a file name using a batch file.

I want the script to check if the file egui.exe is in one of these directories:

Program Files\ESET\ESET Security\
Program Files (86)\ESET\ESET Security\

If the file EXIST then the script should do nothing.
If the file NOT EXIST then it should start Eset Endpoint installation in quiet mode from my server:

msiexec /i myserver\eea_nt64_enu.msi /qb 

I wrote something, but I'm not sure if it is correct and complete:

If exist "C:\program files\ESET\ESET Security\egui.exe" echo ?
else 
call msiexec /i myserver\eea_nt64_enu.msi /qb

Again, I do not want the script to load the installation if the file exists. If the executable exists in that folder, then the batch file should do nothing.

How to check existence of executable in Program files and Program files (x86)?

How to check what version each one has?

If the version of the AntiVirus is old (let's say version 4) then it should force it to install AND override the current version?


Solution

  • Open a command prompt window and run if /? and goto /? to understand the following batch file:

    @echo off
    
    rem This first file existence check works on 32-bit and
    rem 64-bit Windows in 32-bit and 64-bit environment.
    if exist "%ProgramFiles%\ESET\ESET Security\egui.exe" goto :EOF
    
    rem On 64-bit Windows run also a file existence check for 32-bit version
    rem of application in standard program files folder for x86 applications.
    if not "%ProgramFiles(x86)%" == "" if exist "%ProgramFiles(x86)%\ESET\ESET Security\egui.exe" goto :EOF
    
    rem If this batch file is executed by 32-bit cmd.exe on 64-bit Windows
    rem there is still not checked if 64-bit version of application exists
    rem in standard program files folder for x64 applications.
    if not "%ProgramW6432%" == "" if exist "%ProgramW6432%\ESET\ESET Security\egui.exe" goto :EOF
    
    rem Other commands to install the application depending on Windows architecture.
    

    Read the Microsoft article WOW64 Implementation Details for the reason of all those IF conditions.

    But I suppose it would be better to check existence of egui.exe by querying application registration registry key of this application installed with a Microsoft Installer package.

    @echo off
    %SystemRoot%\System32\reg.exe QUERY "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\egui.exe" >nul 2>&1
    if not errorlevel 1 goto :EOF
    
    rem Other commands to install the application depending on Windows architecture.
    

    REG exits with value 1 if the specified registry key does not exist and with value 0 on success querying this registry key.

    if not errorlevel 1 means if exit code of previously executed command/application is not greater or equal 1 which means lower than 1 which means for nearly all commands and applications being equal 0.

    I don't have ESET Security package installed and so don't know if egui.exe (or any other executable from this package) is registered according to Microsoft guidelines for Windows application registration.

    For understanding all used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

    • echo /?
    • goto /?
    • if /?
    • reg /?
    • reg query /?
    • rem /?