1

我正在寻找一个 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)

我显然做错了什么。谁能帮我理解这个?

4

2 回答 2

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

于 2018-06-18T13:56:25.483 回答
1

功能更正,现在适用于 VBScript(感谢 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

使用以下代码调用它:

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

MsgBox(CompareFileVersion(file1, file2))

这将返回:

  • 0如果文件是相同的版本
  • 如果 file1 的版本大于(更新)file2,则为1
  • -1如果 file2 的版本大于(更新)file1
于 2018-06-18T18:03:01.347 回答