Понимание этой функции сравнения (VBScript)
Я ищу решение VBScript для сравнения двух версий файлов и нашел следующий код:
' *****************************************************************
' 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
Как использовать эту функцию при установке следующих переменных?
ver1 = "%userprofile%\Desktop\ver1.exe"
ver2 = "%userprofile%\Desktop\ver2.exe"
Я думал, что выполнения следующего будет достаточно, но я выдаю мне ошибку:
result = CompareFileVersion(ver1, ver2)
MsgBox(result)
Я явно делаю что-то не так. Кто может помочь мне понять это?
2 ответа
Прежде всего, функция должна быть объявлена:
Открытая функция CompareFileVersion(strFileName1, strFileName2)
Что касается
ver1 = "%userprofile%\Desktop\ver1.exe"
ver2 = "%userprofile%\Desktop\ver2.exe"
профиль пользователя, полученный нами WScript.Shell
:
Set oShell = CreateObject("WScript.Shell")
strUser = oShell.ExpandEnvironmentStrings("%USERPROFILE%")
ver1 = strUser+"\Desktop\ver1.exe"
Кроме того, удалите все объявления типов, такие как
Как строка
, VBScript имеет только один тип данных, называемый Variant.
Функция исправлена и теперь работает для VBScript (спасибо Михаю Адриану):
' *****************************************************************
' 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
Звоните по следующему коду:
file1 = "C:\somewhere\file1.exe"
file2 = "C:\somewhere\file2.exe"
MsgBox(CompareFileVersion(file1, file2))
Это вернет:
- 0 если файлы одинаковой версии
- 1, если версия файла1 больше (новее), чем файл2
- -1, если версия файла2 больше (новее), чем файл1