View

VBScript : a method to compare two versions

Option Explicit

Function FirstVersionSupOrEqualToSecondVersion(strFirstVersion, strSecondVersion)
	
	Dim arrFirstVersion,  arrSecondVersion, i, iStop, iMax
	Dim iFirstArraySize, iSecondArraySize
	Dim blnArraySameSize : blnArraySameSize = False
	
	If strFirstVersion = strSecondVersion Then
		FirstVersionSupOrEqualToSecondVersion = True
		Exit Function
	End If
	
	If strFirstVersion = "" Then
		FirstVersionSupOrEqualToSecondVersion = False
		Exit Function
	End If
	If strSecondVersion = "" Then
		FirstVersionSupOrEqualToSecondVersion = True
		Exit Function
	End If

	arrFirstVersion = Split(strFirstVersion, "." )
	arrSecondVersion = Split(strSecondVersion, "." )
	iFirstArraySize = UBound(arrFirstVersion)
	iSecondArraySize = UBound(arrSecondVersion)
	
	If iFirstArraySize = iSecondArraySize Then
		blnArraySameSize = True
		iStop = iFirstArraySize
		For i=0 To iStop
			If CInt(arrFirstVersion(i)) < CInt(arrSecondVersion(i)) Then
				FirstVersionSupOrEqualToSecondVersion = False
				Exit Function
			End If
		Next
		FirstVersionSupOrEqualToSecondVersion = True
	Else
		If iFirstArraySize > iSecondArraySize Then
			iStop = iSecondArraySize
		Else
			iStop = iFirstArraySize
		End If
		For i=0 To iStop
			If CInt(arrFirstVersion(i)) < CInt(arrSecondVersion(i)) Then
				FirstVersionSupOrEqualToSecondVersion = False
				Exit Function
			End If
		Next
		If iFirstArraySize > iSecondArraySize Then
			FirstVersionSupOrEqualToSecondVersion = True
			Exit Function
		Else
			For i=iStop+1 To iSecondArraySize
				If CInt(arrSecondVersion(i)) > 0 Then
					FirstVersionSupOrEqualToSecondVersion = False
					Exit Function
				End If
			Next
			FirstVersionSupOrEqualToSecondVersion = True
		End If
	End If
End Function


Dim strFirstVers : strFirstVers = "1.1.0.1.0."
Dim strSecondVers : strSecondVers = "1.1.0.1.0"

If FirstVersionSupOrEqualToSecondVersion(strFirstVers,strSecondVers) Then
	MsgBox strFirstVers & " >= " & strSecondVers
Else
	MsgBox strFirstVers & " < " & strSecondVers
End If

You may also like