i was preparing my company hr software product for microsoft platform test for isv in order to get microsoft certified partner status.

one of the elective requirement must  be complied by our application is requires sql server 2000 sp3 or later. due to this, i wrote an simple program to parse product version (e.g. 8.00.760) and compare with other product version value.

sample :
?dchkver('8.00.760', '8.00.2338')              && return 'a'
?dchkver('8.00.760', '8.00.110')                && return 'b'
?dchkver('8.00.760', '8.00.760')                && return 's'

*)return value : 1. 'a' - tcversiontocompare is later than tcversion
*)                       2. 'b' - tcversiontocompare is earlier than tcversion
*)                       3. 's' - tcversiontocompare is same as tcversion

function dchkver
lparameters tcversion as string, tcversiontocompare as string

local lnmajor as integer, ;
        lnminor as integer, ;
        lnbuild as integer, ;
        lnmajor2 as integer, ;
        lnminor2 as integer, ;
        lnbuild2 as integer, ;
        lnpos as integer, ;
        lnpos2 as integer, ;
        lcresult as string

lnpos   = at('.', tcversion, 1)
lnpos2  = at('.', tcversion, 2)

lnmajor = val(left(tcversion, lnpos + 1))
lnminor = val(substr(tcversion, lnpos + 1, lnpos2- lnpos))
lnbuild = val(substr(tcversion, lnpos2 + 1))

lnpos   = at('.', tcversiontocompare , 1)
lnpos2  = at('.', tcversiontocompare , 2)

lnmajor2= val(left(tcversiontocompare , lnpos + 1))
lnminor2= val(substr(tcversiontocompare , lnpos + 1, lnpos2- lnpos))
lnbuild2= val(substr(tcversiontocompare , lnpos2 + 1))

do case
case lnmajor2 > lnmajor
    lcresult = 'a'
case lnmajor2 = lnmajor and lnminor2 > lnminor
    lcresult = 'a'
case lnmajor2 = lnmajor and lnminor2 = lnminor and lnbuild2 > lnbuild
    lcresult = 'a'
case lnmajor2 = lnmajor and lnminor2 = lnminor and lnbuild2 = lnbuild
    lcresult = 's'
otherwise
    lcresult = 'b'
endcase

return lcresult

if anyone has simpler way for this function and willing to share, please don't hesitate to email me. 🙂

6 Responses to Program to compare product version

  • Eric says:

    FUNCTION dChkVer

    LPARAMETERS tcVersion AS String, tcVersionToCompare AS String

    lnVersion = INT(VAL(CHRTRAN(tcVersion,".","")))

    lnVersionToCompare = INT(VAL(CHRTRAN(tcVersionToCompare,".","")))

    RETURN IIF(lnVersion = lnVersionToCompare, "S", IIF(lnVersion > lnVersionToCompare, "B", "A"))

    ENDFUNC

  • kkchan says:

    Eric,

    Thank you for you code. It is extremely simple. However, I found that this code doesn’t handle scenario as below:

    ?dChkVer(‘8.00.760’, ‘8.1.760’)

    8.1.760 is later than 8.00.760

  • davidfung says:

    The shorter version as-is does not work for all cases.  For example:

    dChkVer(‘8.00.760’, ‘7.00.2338’) returns ‘A’ instead of ‘B’…

  • calloatti says:

    How about something like this:

    lcReq = "8.00.760.1"

    lcPre = "8.00.2338"

    ?dChkVer(lcReq, lcPre)

    FUNCTION dChkVer

    *!* Works for versions up to "9999.9999.9999.9999"

    *!* If version present = version required returns 0

    *!* If version present > version required returns 1

    *!* If version present < version required returns -1

    Lparameters tcVerRequired, tcVerPresent

    Local lcPart, lcVer1, lcVer2, lnRet

    m.lcVer1 = []

    m.lcVer2 = []

    For m.lnWord = 1 To 4

    m.lcPart = Getwordnum(m.tcVerRequired, lnWord, [.])

    m.lcPart = Padl(m.lcPart, 4, [0])

    m.lcVer1 = m.lcVer1 + m.lcPart

    Endfor

    For m.lnWord = 1 To 4

    m.lcPart = Getwordnum(m.tcVerPresent, lnWord, [.])

    m.lcPart = Padl(m.lcPart, 4, [0])

    m.lcVer2 = m.lcVer2 + m.lcPart

    Endfor

    Do Case

    Case m.lcVer1 = m.lcVer2

    m.lnRet = 0

    Case m.lcVer1 < m.lcVer2

    m.lnRet = 1

    Case m.lcVer1 > m.lcVer2

    m.lnRet = -1

    Endcase

    ?m.lcVer1, m.lcVer2

    Return m.lnRet

  • kkchan says:

    collatti,

    Thank you for your idea. I borrowed your idea to re-write my function. It can handle unlimited “.” with shorter code. Here is the revised code :

    ?dChkVer2(‘8.00.760’, ‘8.00.2338’)              && Return ‘A’

    ?dChkVer2(‘8.00.760’, ‘8.00.110’)               && Return ‘B’

    ?dChkVer2(‘8.00.760’, ‘8.00.760’)               && Return ‘S’

    ?dChkVer2(‘8.00.760’, ‘7.00.2338’)              && Return ‘B’

    ?dChkVer2(‘8.00.760’, ‘8.1.760’)                && Return ‘A’

    ?dChkVer2(‘8.00.760.1’, ‘8.00.2338’)            && Return ‘A’

    ?dChkVer2(‘8.00.760.1’, ‘8.00.760.8’)           && Return ‘A’

    FUNCTION dchkver2

    LPARAMETERS tcVersion AS String, tcVersionToCompare AS String

    LOCAL lnPoint AS Integer, ;

    lnNumber AS Integer, ;

    lnNumber2 AS Integer, ;

    lcResult AS String, ;

    lnCount AS Integer

    lnPoint = MAX(OCCURS(“.”, tcVersion), OCCURS(“.”, tcVersionToCompare)) + 1

    lcResult= “S”

    FOR lnCount = 1 TO lnPoint

    lnNumber = VAL(GETWORDNUM(tcVersion, lnCount, “.”))

    lnNumber2= VAL(GETWORDNUM(tcVersionToCompare, lnCount, “.”))

    DO CASE

    CASE lnNumber > lnNumber2

    lcResult = “B”

    EXIT

    CASE lnNumber2 > lnNumber

    lcResult = “A”

    EXIT

    ENDCASE

    NEXT

    RETURN lcResult

    Thank you

  • chj124 says:

    heres code i use, works with any version by converting it to a number and then compairing.

    i create update files with the version in the name… ie…  DR3_Update.0.1.95.exe

    the version must match the build of youe exe file.

    *************************************

    * check for updates

    *************************************

    LOCAL laFileInfo2, lnRes, lcVer1, lcVer2, lcVer3, lnXX, lcFileToRun

    LOCAL ARRAY laFileInfo1(15), laDirList(100)

    *************************************

    * get this files version

    *************************************

    lnRes=AGETFILEVERSION(laFileInfo1, ‘dr3.exe’)

    IF lnRes <> 0

    lcTempVer1 = laFileInfo1(4)

    ELSE

    lcTempVer1 = ‘0.0.0’

    ENDIF

    lcVer1 = REPLICATE(‘0’, 6 – LEN(LEFT(lcTempVer1, AT(‘.’, lcTempVer1)-1))) + LEFT(lcTempVer1, AT(‘.’, lcTempVer1)-1)

    lcVer1 = lcVer1  + REPLICATE(‘0′, 6 – LEN(STREXTRACT(lcTempVer1,’.’, ‘.’,1))) + STREXTRACT(lcTempVer1,’.’, ‘.’,1)

    lcVer1 = lcVer1  + REPLICATE(‘0′, 6 – LEN(STREXTRACT(lcTempVer1,’.’, ‘.’,2,2))) + STREXTRACT(lcTempVer1,’.’, ‘.’,2,2)

    *************************************

    * check for the latest update file

    *************************************

    lcFileToRun = ”

    lnRes=ADIR(laDirList, oAppData.SetupPath + ‘DR3_Update*.exe’)

    FOR lnXX = 1 TO lnRes

    lcVer2 = REPLICATE(‘0′, 6 – LEN(STREXTRACT(laDirList(lnXX,1),’.’, ‘.’,1))) + STREXTRACT(laDirList(lnXX,1),’.’, ‘.’,1)

    lcVer2 = lcVer2 + REPLICATE(‘0′, 6 – LEN(STREXTRACT(laDirList(lnXX,1),’.’, ‘.’,2))) + STREXTRACT(laDirList(lnXX,1),’.’, ‘.’,2)

    lcVer2 = lcVer2 + REPLICATE(‘0′, 6 – LEN(STREXTRACT(laDirList(lnXX,1),’.’, ‘.’,3,2))) + STREXTRACT(laDirList(lnXX,1),’.’, ‘.’,3,2)

    IF VAL(lcVer2) > VAL(lcVer1)

    lcFileToRun = oAppData.SetupPath + laDirList(lnXX,1) && run update file if newer

    lcVer1 = vcVer2

    ENDIF

    ENDFOR

    RETURN lcFileToRun

Leave a Reply

Your email address will not be published. Required fields are marked *