0

I´m working on a script that automatically downloads a software. The software runs on the latest version of VMware Workstation. Right now it is version 16.1.0.

I want vmware.exe version 16 to be installed when there is no vmware.exe installed at all or the version is not at least 16.1.0.683.

I found a few options, but none of them seem to work for me.

The not working code I have so far:

if exist "C:\Program Files (x86)\VMware\VMware Workstation\vmware.exe VER 16.1.0.683" (
    echo Version installed
) else (
    echo Installation takes some minutes. Don't interrupt!
    %~dp0\..\downloads\VMware-workstation-full-16.1.0-17198959.exe /s /v /qn EULAS_AGREED=1 AUTOSOFTWAREUPDATE=0 DATACOLLECTION=0
)

It always runs the command in the else statement.

Any ideas?

2
  • 3
    You have not got a file named vmware.exe VER 16.1.0.683 on your system, that is why it always fails. Your file is named vmware.exe. There are plenty of questions on this site where the version of an executable file needed to be determined, have you used the search facility?
    – Compo
    Apr 13, 2021 at 10:08
  • The help file for the IF command is pretty clear what its intended use isfor. EXIST filename Specifies a true condition if the specified filename exists. Where did you find an example of trying to use a version number with that command?
    – Squashman
    Apr 13, 2021 at 13:51

1 Answer 1

0

I suggest to use this batch file for the VMware Workstation installation and update task:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "AppFileName=vmware.exe"
set "AppMinVersion=16.1.0.683"
set "AppName=VMware Workstation"

for /F "skip=1 tokens=2*" %%I in ('%SystemRoot%\System32\reg.exe QUERY "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\%AppFileName%" /ve 2^>nul') do if "%%I" == "REG_SZ" (
    if not exist "%%J" (
        echo %AppName% is registered to be installed in the folder:
        echo(
        echo "%%~dpJ"
        echo(
        echo But the file %AppFileName% does not exist.
        echo(
        echo Installing %AppName% ...
        echo(
        goto InstallVMware
    )
    set "AppFullName=%%J"
    goto GetVersion
)
echo %AppName% is not installed and registered. Installing it ...
echo(
goto InstallVMware

:GetVersion
set "EscapedFileName=%AppFullName:\=\\%"
set "EscapedFileName=%EscapedFileName:'=\'%"
for /F "usebackq delims=" %%I in (`%SystemRoot%\System32\wbem\wmic.exe DataFile where "Name='%EscapedFileName%'" GET Version /VALUE 2^>nul`) do for /F "tokens=1* delims==" %%J in ("%%I") do set "VersionVMware=%%K" & goto CheckVersion
echo ERROR: Failed to determine version of installed %AppName% executable:
echo(
echo        "%AppFullName%"
echo(
echo Please report this error to ... for a manual investigation.
echo(
echo There is done nothing regarding to %AppName% installation.
pause
goto EndBatch

:CheckVersion
for /F "tokens=1-4 delims=." %%G in ("%VersionVMware%") do set "VersionMajor=%%G" & set "VersionMinor=%%H" & set "VersionPatch=%%I" & set "VersionBuild=%%J"
for /F "tokens=1-4 delims=." %%G in ("%AppMinVersion%") do set "MinVersionMajor=%%G" & set "MinVersionMinor=%%H" & set "MinVersionPatch=%%I" & set "MinVersionBuild=%%J"
rem For the following version number comparisons is expexted that the version
rem string does not contain numbers with leading zeros like 08 or 009 or 014
rem as such numbers would be interpreted as octal numbers by command IF.
if %VersionMajor% LSS %MinVersionMajor% goto UpdateVMware
if %VersionMajor% GTR %MinVersionMajor% goto NoInstall
if %VersionMinor% LSS %MinVersionMinor% goto UpdateVMware
if %VersionMinor% GTR %MinVersionMinor% goto NoInstall
if %VersionPatch% LSS %MinVersionPatch% goto UpdateVMware
if %VersionPatch% GTR %MinVersionPatch% goto NoInstall
if %VersionBuild% LSS %MinVersionBuild% goto UpdateVMware

:NoInstall
echo %AppName% is already installed in version %VersionVMware%.
goto EndBatch

:UpdateVMware
echo %AppName% is installed in version %VersionVMware%.
echo An update is necessary, installing it ...
echo(

:InstallVMware
echo The installation of %AppName% %AppMinVersion% takes some minutes.
echo Please don't interrupt the installation process!
echo(
"%~dp0..\downloads\VMware-workstation-full-16.1.0-17198959.exe" /s /v /qn EULAS_AGREED=1 AUTOSOFTWAREUPDATE=0 DATACOLLECTION=0

:EndBatch
endlocal

The batch file first checks the application registration of VMware Workstation to find out if it is installed at all and in which folder vmware.exe is installed on the current machine. The folder %ProgramFiles(x86)%\VMware\VMware Workstation is just the default installation folder of VMware Workstation. It can be installed to any other folder during a manual installation.

If vmware.exe is registered as installed application, the batch file checks next if the executable really still exists in registered installation folder. Otherwise the installation is corrupt and a new installation is necessary to fix this issue.

Next the batch file determines the version of vmware.exe. That could fail if there is one or more commas in path which is very unusual, but nevertheless not impossible. In this case the user running the batch file should report this issue to somebody for a manual investigation.

The version of installed VMware Workstation is split up on the dots. Then is compared the version of installed VMware Workstation with the version which should be installed with the batch file. There is just an information output on installed VMware Workstation is already of expected version 16.1.0.683 or even a newer version.

Otherwise the installation is started with the parameters defined by you to update the existing installation or first time install VMware Workstation or fix the corrupted installation.

Note: The path of the batch file referenced with %~dp0 always ends with a backslash. For that reason never concatenate %~dp0 with an additional backlash with a file, folder or wildcard pattern as this results in \\ in finally expanded argument string which the Windows file management must correct back to \ before passing the argument string to the file system.

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

  • call /? ... explains how to reference batch file arguments as done with %~dp0
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • reg /?
  • reg query /?
  • set /?
  • setlocal /?
  • wmic /?
  • wmic datafile /?
  • wmic datafile get /?

See also:

Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which starts in background with %ComSpec% /c one more command process and passes the command line as argument strings to this command process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.