1

I'm looking for a VBScript solution to compare two file versions and found the following code:

' *****************************************************************
' CompareFileVersion()
' Author: Vincil.Bishop@dhs.state.tx.us
' Date: 6/30/03
'
' This function compares the version numbers of two files and returns the following results:
' -1 = File Version 2 is greater than File Version 1
' 0 = Versions are the same
' 1 = File version 1 is greater than File Version 2
' ****************************************************************************************************

Public Function CompareFileVersion(strFileName1 As String, strFileName2 As String) As Integer

    ' Our result
    ' -1 = File Version 2 is greater than File Version 1
    ' 0 = Versions are the same
    ' 1 = File version 1 is greater than File Version 2
    Dim intResult

    Dim strFileVersion1
    Dim strFileVersion2
    Dim strAryFileVersion1() As String
    Dim strAryFileVersion2() As String

    Dim fs
    Set fs = CreateObject("Scripting.FileSystemObject")

    ' Let's initialize our result with 0
    intResult = 0

    strFileVersion1 = fs.getfileversion(strFileName1)
    strFileVersion2 = fs.getfileversion(strFileName2)

    'Split the two supplied file versions by the "." character
    strAryFileVersion1() = Split(strFileVersion1, ".")
    strAryFileVersion2() = Split(strFileVersion2, ".")

    For i = 0 To UBound(strAryFileVersion1())

        If strAryFileVersion1(i) > strAryFileVersion2(i) Then

            intResult = 1

        ElseIf strAryFileVersion1(i) < strAryFileVersion2(i) Then

            intResult = -1

        End If

        'If we have found that the result is not > or <, no need to proceed
        If intResult <> 0 Then Exit For

    Next

    If UBound(strAryFileVersion2) > UBound(strAryFileVersion1) _
    And strAryFileVersion2(UBound(strAryFileVersion2)) <> 0 Then intResult = -1

    CompareFileVersion = intResult

End Function

How do I use this function, when setting the following variables?

ver1 = "%userprofile%\Desktop\ver1.exe"
ver2 = "%userprofile%\Desktop\ver2.exe"

I thought doing the following would suffice, but I gives me a error:

result = CompareFileVersion(ver1, ver2)
MsgBox(result)

I obviously am doing something wrong. Who can help me understand this?

WatskeBart
  • 119
  • 13

2 Answers2

2

First of all, the function should be declared:

Public Function CompareFileVersion(strFileName1, strFileName2)

As for

ver1 = "%userprofile%\Desktop\ver1.exe"
ver2 = "%userprofile%\Desktop\ver2.exe"

the user profile us obtained using WScript.Shell:

Set oShell = CreateObject("WScript.Shell")
strUser = oShell.ExpandEnvironmentStrings("%USERPROFILE%")

ver1 = strUser+"\Desktop\ver1.exe"

Also, remove all type declarations, like

As String

. VBScript has only one data type called a Variant.

Mihai Adrian
  • 645
  • 6
  • 17
  • Thanks for the quick response, and you are right. I removed the type declarations and some other errors in the arrays. Now everything is working fine. – WatskeBart Jun 18 '18 at 17:54
1

Function corrected and now works for VBScript (thanks to Mihai Adrian):

' *****************************************************************
' CompareFileVersion()
' Author: Vincil.Bishop@dhs.state.tx.us
' Date: 6/30/03
'
' This function compares the version numbers of two files and returns the following results:
' -1 = File Version 2 is greater than File Version 1
' 0 = Versions are the same
' 1 = File version 1 is greater than File Version 2
' ****************************************************************************************************

Public Function CompareFileVersion(strFileName1, strFileName2)

    ' Our result
    ' -1 = File Version 2 is greater than File Version 1
    ' 0 = Versions are the same
    ' 1 = File version 1 is greater than File Version 2
    Dim intResult

    Dim strFileVersion1
    Dim strFileVersion2
    Dim strAryFileVersion1
    Dim strAryFileVersion2

    Dim fs
    Set fs = CreateObject("Scripting.FileSystemObject")

    ' Let's initialize our result with 0
    intResult = 0

    strFileVersion1 = fs.getfileversion(strFileName1)
    strFileVersion2 = fs.getfileversion(strFileName2)

    'Split the two supplied file versions by the "." character
    strAryFileVersion1 = Split(strFileVersion1, ".")
    strAryFileVersion2 = Split(strFileVersion2, ".")

    For i = 0 To UBound(strAryFileVersion1)

        If strAryFileVersion1(i) > strAryFileVersion2(i) Then

            intResult = 1

        ElseIf strAryFileVersion1(i) < strAryFileVersion2(i) Then

            intResult = -1

        End If

        'If we have found that the result is not > or <, no need to proceed
        If intResult <> 0 Then Exit For

    Next

    If UBound(strAryFileVersion2) > UBound(strAryFileVersion1) _
    And strAryFileVersion2(UBound(strAryFileVersion2)) <> 0 Then intResult = -1

    CompareFileVersion = intResult

End Function

Calling it with following code:

file1 = "C:\somewhere\file1.exe"
file2 = "C:\somewhere\file2.exe"

MsgBox(CompareFileVersion(file1, file2))

This will return:

  • 0 if files are the same version
  • 1 if version of file1 is greater (newer) than file2
  • -1 if version of file2 is greater (newer) than file1
WatskeBart
  • 119
  • 13