VB and VBA Users Source Code: Compare two file version numbers
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Compare two file version numbers
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Thursday, May 03, 2001
Hits:
656
Category:
Files/Directories/IO
Article:
When installing application, dlls, OCX's etc, it is useful to determine which file version is the latest. The version numbers normally take the form "1.201.30", with the numbers given in descending order of importance. The following code parses the version number strings of two files and returns the file number which has the latest version number. Note, use this code in conjuction with the article which demonstrated how to return file version numbers: http://www.vbusers.com/code/codeget.asp?ThreadID=58&PostID=1&NumReplies=1 Code: Option Explicit 'Purpose : Check two file version numbers 'Inputs : sFile1. The first file to check the version number of. ' sFile1. The second file to check the version number of. 'Outputs : Returns 1 if the first file has the lastest version number. ' Returns 2 if the second file has the lastest version number. ' Returns -1 if error occurs. 'Author : Andrew Baker 'Date : 25/03/2000 'Notes : If the files contains a different length version parts eg. ' File1 = "1.1.03" and File2 = "1.1.4" ' Then File2's version number will be consider to be File2 = "1.1.40" Function FileVersionCompare(sFile1 As String, sFile2 As String) As Long Dim lFile1Ver As Long, lPos1 As Long, lOldPos1 As Long, lLen1 As Long Dim lFile2Ver As Long, lPos2 As Long, lOldPos2 As Long, lLen2 As Long Dim lVerLen As Long On Error GoTo ErrFailed lLen1 = Len(sFile1) lLen2 = Len(sFile2) lPos1 = 1 lPos2 = 1 Do 'Find the position of the file 1's decimal seperator lPos1 = InStr(lPos1, sFile1, ".") If lPos1 = 0 Then lPos1 = lLen1 + 1 End If 'Find the position of the file 2's decimal seperator lPos2 = InStr(lPos2, sFile2, ".") If lPos2 = 0 Then lPos2 = lLen2 + 1 End If 'Determine the length of the version part If lPos1 - lOldPos1 > lPos2 - lOldPos2 Then lVerLen = lPos1 - lOldPos1 - 1 Else lVerLen = lPos2 - lOldPos2 - 1 End If 'Determine the value of the version part lFile1Ver = CLng(Mid$(sFile1, lOldPos1 + 1, lPos1 - lOldPos1 - 1)) * 10 ^ (lVerLen - (lPos1 - lOldPos1 - 1) + 1) / 10 lFile2Ver = CLng(Mid$(sFile2, lOldPos2 + 1, lPos2 - lOldPos2 - 1)) * 10 ^ (lVerLen - (lPos2 - lOldPos2 - 1) + 1) / 10 If lFile1Ver > lFile2Ver Then 'File1 is the latest FileVersionCompare = 1 Exit Do ElseIf lFile1Ver < lFile2Ver Then 'File2 is the latest FileVersionCompare = 2 Exit Do End If If lPos1 >= lLen1 Or lPos2 >= lLen2 Then 'Finished checking version parts Exit Do End If 'Store the position of the last decimal seperator lOldPos1 = lPos1 lOldPos2 = lPos2 'Look at the next version part lPos1 = lPos1 + 1 lPos2 = lPos2 + 1 Loop Exit Function ErrFailed: FileVersionCompare = -1 On Error GoTo 0 End Function 'Demonstration routine Sub Test() Dim lRetVal As Long lRetVal = FileVersionCompare("1.2.03", "1.2.04") Select Case lRetVal Case 1 MsgBox "File 1 is the latest" Case 2 MsgBox "File 2 is the latest" Case -1 MsgBox "Error occured" End Select End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder