Matt Jackson
Matt Jackson

Reputation: 158

batch file for checking program version to see if it should be upgraded

I am attempting to write a batch file to check a program version to see if it needs to be upgraded, or not. The issue is that some versions will not upgrade properly, so if it is between a certain version range it needs to be uninstalled then reinstalled before upgrading. I'm trying to make the return from the WMIC command a variable that I can use to compare against the current version. I'm not sure if this is even the most efficient way of handling this or not. The code is incomplete, because I haven't gotten it to work, but what I have so far is:

    set latestVersion=11.0.0.8
    set currentVersion=('wmic datafile where name="D:\\Program Files\\Adobe\\Reader 11.0\\Reader\\AcroRd32.exe"' get version)

    if %currentVersion%=%latestVersion% goto noUpdate
    if %currentVersion%>='11.0.0.3' goto reinstall
    if %currentVersion%<='11.0.0.2' goto update

Upvotes: 1

Views: 1436

Answers (2)

npocmaka
npocmaka

Reputation: 57252

@ECHO off
   set latestVersion=11.0.0.8
   set minVersion=11.0.0.3
   for /f "tokens=1,2,3,4 delims=." %%A in ("%latestVersion%") do set numirized_version=%%A%%B%%C%%D
   echo %numirized_version%
      for /f "tokens=1,2,3,4 delims=." %%A in ("%minVersion%") do set min_numirized_version=%%A%%B%%C%%D
   echo %min_numirized_version%
   for /f "usebackq tokens=2,3,4,5 delims=,." %%a in (`"wmic datafile where name='D:\\Program Files\\Adobe\\Reader 11.0\\Reader\\AcroRd32.exe' get version /format:csv"`) do (
        set ver=%%a%%b%%c%%d
    )

    if 1%ver% EQU 1%numirized_version% goto :no_update
    if 1%ver% LSS 1%min_numirized_version% goto :reinstall
    if 1%ver% GEQ 1%min_numirized_version% if if 1%ver% LSS 1%numirized_version% goto :update

Upvotes: 0

Torqane
Torqane

Reputation: 146

Well, there are a few issues here. First of all you can't directly use the result of a command in a SET statement; you have to do it with a FOR loop such as, in your case:

for /f "skip=2 tokens=2 delims== " %%i in ('wmic datafile where name^=^"D:\\Program Files\\Adobe\\Reader 11.0\\Reader\\AcroRd32.exe^" get version /value') do set currentversion=%%i

Then you can use %currentversion% in comparisons. However you won't be able to use the comparison operators as you've done here. For equality you use a double equals == and for your other two you'd use GEQ and LEQ. Also you wouldn't use quotes around the values because that's not what's going to come out of wmic, or use them on both sides of the operator. The other issue is that you're comparing strings that could be of different lengths unless you control that, and 4.1.0.1 is greater than 11.2.5.7 in this sort of batch comparison. If that's a problem you may want to switch to something like VBScript where you're in more control.

The FOR line is a bit complex, but basically you're looping through the output from the wmic command, skipping the first 2 lines, and only looking at the second delimited value of that third line (using = and space as possible delimters).

Also in the command itself you have to escape the double-quotes and the equal sign, and that's done by preceding them with the ^ caret character. If you're running it on a 64-bit operating system and your reader is in

Program Files (x86)

then you have to escape those parentheses too, as in

Program Files ^(x86^).

So after all those words up there, here's a fixed version of your code (though untested precisely, because my AcroRD32.exe isn't where yours is, but that's what reminded me of the (x86) thing), which as you say won't do anything now except complain about missing labels, but this should get you going:

set latestVersion=11.0.0.8
for /f "skip=2 tokens=2 delims== " %%i in ('wmic datafile where name^=^"D:\\Program Files\\Adobe\\Reader 11.0\\Reader\\AcroRd32.exe^" get version /value') do set currentversion=%%i

if %currentVersion%==%latestVersion% goto noUpdate
if %currentVersion% GEQ 11.0.0.3 goto reinstall
if %currentVersion% LEQ 11.0.0.2 goto update

Reference if /? at the command line to get the other comparison operators.

Upvotes: 1

Related Questions