tags:

views:

292

answers:

3

I have a VBScript that checks for the existance of a file in a directory on a remote machine. I am looking to retrieve the "Product Version" for said file (NOT "File Version"), but I can't seem to figure out how to do that in VBScript.

I'm currently using Scripting.FileSystemObject to check for the existence of the file.

Thanks much.

A: 

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

Michael R Felkins
+1  A: 

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

You may also find the following post helpful.

Garett
+2  A: 

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 objShell, objFolder, objFolderItem, i 
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

Maputi