How to Retrieve a File's “Product Version” in VBScript

前端 未结 5 802
你的背包
你的背包 2020-12-16 20:25

I have a VBScript that checks for the existence of a file in a directory on a remote machine. I am looking to retrieve the \"Product Version\" for said fil

相关标签:
5条回答
  • 2020-12-16 20:54

    I use a function that is slightly modified from the previous example. The function takes the path and file name and returns the "Product Version"

    Function GetProductVersion (sFilePath, sProgram)
    Dim FSO,objShell, objFolder, objFolderItem, i 
    Set FSO = CreateObject("Scripting.FileSystemObject")
    If FSO.FileExists(sFilePath & "\" & sProgram) Then
        Set objShell = CreateObject("Shell.Application")
        Set objFolder = objShell.Namespace(sFilePath)
        Set objFolderItem = objFolder.ParseName(sProgram)
        Dim arrHeaders(300)
        For i = 0 To 300
            arrHeaders(i) = objFolder.GetDetailsOf(objFolder.Items, i)
            'WScript.Echo i &"- " & arrHeaders(i) & ": " & objFolder.GetDetailsOf(objFolderItem, i)
            If lcase(arrHeaders(i))= "product version" Then
                GetProductVersion= objFolder.GetDetailsOf(objFolderItem, i)
                Exit For
            End If
        Next
    End If
    End Function
    

    I've found that the position of the attributes has occasionally changes (not sure why) in XP and Vista so I look for the "product version" attribute and exit the loop once it's found. The commented out line will show all the attributes and a value if available

    0 讨论(0)
  • 2020-12-16 20:56

    I don't think you can do it in vbScript. I could be wrong on that.

    Here is as link to a powershell script that does what you are asking.

    link text

    Here is what the output for my windows dir looks like.

        PS Scripts:\> ls c:\windows\*.exe | .\get-fileversion.ps1
    
    ProductVersion   FileVersion      FileName
    --------------   -----------      --------
    2.7.3.0          2.7.3.0          C:\windows\agrsmdel.exe
    
    0 讨论(0)
  • 2020-12-16 21:07

    You can use the Shell.Namespace to get the extended properties on a file, one of which is the Product Version. The GetDetailsOf function should work. You can test with the following code to get an idea:

    Dim fillAttributes(300)
    
    Set shell = CreateObject("Shell.Application")
    Set folder = shell.Namespace("C:\Windows")
    
    Set file = folder.ParseName("notepad.exe")
    
    For i = 0 to 299
        Wscript.Echo i & vbtab & fillAttributes(i) _
            & ": " & folder.GetDetailsOf(file, i) 
    Next
    

    One thing to be aware of:

    The extended properties of a file differs between versions of Windows. Hence, the product version index numbers changes based on the version of Windows you are using. You can use the code above to determine what they are. From my testing, I believe they are as follows:

    • Window XP - 39
    • Windows Vista - 252
    • Windows 7 - 268
    • Windows 2008 R2 SP1 - 271
    • Windows 2012 R2 - 285

    You may also find the following post helpful.

    0 讨论(0)
  • 2020-12-16 21:07
    ' must explicitly declare all variables
    Option Explicit
    ' declare global variables
    Dim aFileFullPath, aDetail
    ' set global variables
    aFileFullPath = "C:\Windows\Notepad.exe"
    aDetail = "Product Version"
    ' display a message with file location and file detail
    WScript.Echo ("File location: " & vbTab & aFileFullPath & vbNewLine & _
    aDetail & ": " & vbTab & fGetFileDetail(aFileFullPath, aDetail))
    ' make global variable happy. set them free
    Set aFileFullPath = Nothing
    Set aDetail = Nothing
    ' get file detail function. created by Stefan Arhip on 20111026 1000
    Function fGetFileDetail(aFileFullPath, aDetail)
    ' declare local variables
    Dim pvShell, pvFileSystemObject, pvFolderName, pvFileName, pvFolder, pvFile, i
    ' set object to work with files
    Set pvFileSystemObject = CreateObject("Scripting.FileSystemObject")
    ' check if aFileFullPath provided exists
    If pvFileSystemObject.FileExists(aFileFullPath) Then
    ' extract only folder & file from aFileFullPath
    pvFolderName = pvFileSystemObject.GetFile(aFileFullPath).ParentFolder
    pvFileName = pvFileSystemObject.GetFile(aFileFullPath).Name
    ' set object to work with file details
    Set pvShell = CreateObject("Shell.Application")
    Set pvFolder = pvShell.Namespace(pvFolderName)
    Set pvFile = pvFolder.ParseName(pvFileName)
    ' in case detail is not detected...
    fGetFileDetail = "Detail not detected"
    ' parse 400 details for given file
    For i = 0 To 399
    ' if desired detail name is found, set function result to detail value
    If uCase(pvFolder.GetDetailsOf(pvFolder.Items, i)) = uCase(aDetail) Then
    fGetFileDetail = pvFolder.GetDetailsOf(pvFile, i)
    End If
    Next
    ' if aFileFullPath provided do not exists
    Else
    fGetFileDetail = "File not found"
    End If
    ' make local variable happy. set them free
    Set pvShell = Nothing
    Set pvFileSystemObject = Nothing
    Set pvFolderName = Nothing
    Set pvFileName = Nothing
    Set pvFolder = Nothing
    Set pvFile = Nothing
    Set i = Nothing
    End Function
    
    0 讨论(0)
  • 2020-12-16 21:16
    Wscript.Echo CreateObject("Scripting.FileSystemObject").GetFileVersion("C:\Windows\notepad.exe")
    
    0 讨论(0)
提交回复
热议问题