[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3290
  • Last Modified:

How do I obtain the 'product version' of an executable using vbscript?

I am trying to obtain the product version a EXE file using vbscript.  The "product version" is visable in Windows XP by right-clicking on the EXE file, go to the Version tab and the "product version" is listed as one of the item names.   Anyone know how to obtain this version info using a vbs file.

0
grummite
Asked:
grummite
  • 2
1 Solution
 
yehudahaCommented:
you can try this
edit this line to the path and file name you need
notice every backslash need to be double
example: c:\\windows\\system32\\ping.exe

 ("Select * from CIM_Datafile Where name = 'C:\\temp\\setup.exe'")

one more thing file version and product version are diffrent attributes but in most cases are the same
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_Datafile Where name = 'C:\\temp\\setup.exe'")
For Each objFile in colFiles
    Wscript.Echo "Version: " & objFile.Version
Next

Open in new window

0
 
grummiteAuthor Commented:
Thanks for the assistance but I was trying to find the "product version" not the "file version".  The executable I am querying has different values for these two properties.  The "product version" is visable in Windows XP by right-clicking on the EXE file, go to the Version tab and the "product version" is listed as one of the item names.
0
 
yehudahaCommented:
source:

http://www.jsware.net/jsware/scripts.php5#filfol

here:
       '-- This script includes two classes used toi get file version info, from PE files. (EXE, DLL, OCX)
       '-- The demo script shows how it works. Just drop a PE file onto this file.
       '-- To use the classes in other scripts, copy everything beow the line where it
       '-- says: ' Class CVersionInfo ------------------------
       '-- Then paste that text at the bottom of your script. You will then be able
       '-- to create the class object and use it.
 
 
'-- possible version info. values: 
'  CompanyName FileDescription FileVersion InternalName LegalCopyright OriginalFilename ProductName ProductVersion 
' The script can find any values, but these are the ones that are defined as standard in MS file version info.
 
 
'------------ begin sample script ---------------------------------
Dim ClsVI, VI, s, iRet, Arg, s1, s2
 
Arg = InputBox("Enter File Path . e.g : c:\test.exe")
   If Arg = "" Then
     MsgBox "Drop an EXE, DLL, or OCX file onto this script.", 64
     WScript.Quit
   End If
     
 Set ClsVI = New CVersionInfo
 Set VI = ClsVI.GetFileVersionInfo(Arg)
   Select Case VI.State
     Case 0
       s1 = "Company: " & VI.Value("CompanyName")
       s1 = s1 & vbCrLf & "Version: " &  VI.Value("FileVersion")
       s1 = s1 & vbCrLf & "Description: " &  VI.Value("FileDescription")
     Case 1
        s1 = "Invalid file path." 
     Case 2
        s1 = "No .rsrc table listed in section table."
     Case 3
        s1 = "Failed to find version info." 
     Case 4
        s1 = "File is not a PE file."
     Case 5
        s1 = "File is a 16-bit executable. There is no file version info. for 16-bit."
   End Select
   
  MsgBox s1, 64
  
 Set VI = Nothing
Set ClsVI = Nothing
WScript.quit
 
'------------------- end sample script ---------------------------------------
 
 
 
 
'
'----------------////////////  --- BEGIN Classes HERE ---  /////////////////////-------------------------------------------
 
'-- note: variable names in classes are deliberately clunky in order to avoid conflict with possible
'-- global variables in script.
 
'------------ copy and paste from below this line to use these classes elsewhere. ----------------------------------
 
' Class CVersionInfo ------------------------------------------------------
 
Class CVersionInfo
   Private FSOcvi, TScvi, ANumscvi, VIcvi
   
  Private Sub Class_Initialize()
    On Error Resume Next
         Set FSOcvi = CreateObject("Scripting.FileSystemObject")
  End Sub
          
  Private Sub Class_Terminate()
    On Error Resume Next
      Set TScvi = Nothing   '-- just in case.
      Set FSOcvi = Nothing
      Set VIcvi = Nothing
  End Sub
  
  '-- The public function in this class: GetFileVersionInfo -----------------------------------------  
Public Function GetFileVersionInfo(sFilePath)  
Dim ARetcvi, s1cvi, Pt1cvi, sRes, sBcvi, A1cvi, A4cvi(3), A2cvi(1), LocRes, VLocRes, SizeRes, iOffSet, Boocvi, sVerString, sMarker
Dim iNum1cvi, iNum2cvi, iReadPt, iNum3cvi, LocAspack, VLocAspack, VIOffset, ReadOffset, BooAspack
   On Error Resume Next
     Set VIcvi = Nothing
     Set VIcvi = New VSInfo
               If (FSOcvi.FileExists(sFilePath) = False) Then
                    VIcvi.Init "", 1
                    Set GetFileVersionInfo = VIcvi  'bad path.
                    Exit Function
               End If
       sRes = ".rsrc"
       sVerString = "VS_VER"
       BooAspack = False
           
   Set TScvi = FSOcvi.OpenTextFile(sFilePath, 1)
       s1cvi = TScvi.Read(2048) '-- Read first 2 KB.
       TScvi.Close
   Set TScvi = Nothing    
      A1cvi = GetArray(Mid(s1cvi, 61, 2))  '-- get number value at offset 60 that points to PE signature address.
      iNum1cvi = (GetNumFromBytes(A1cvi) + 1)     '-- get offset of "PE00"
      sBcvi = GetByteString(s1cvi, False)  '-- get a workable string with Chr(0) replaced by "*".       
       sMarker = Mid(sBcvi, iNum1cvi, 4) 
         If (sMarker <> "PE**") Then
                 If Left(sMarker, 2) = "NE" Then
                      VIcvi.Init "", 5
                    Set GetFileVersionInfo = VIcvi  '-- 16 bit.
                 Else 
                      VIcvi.Init "", 4
                     Set GetFileVersionInfo = VIcvi  '-- no PE signature found.
                 End If   
             Exit Function 
         End If
          
     Pt1cvi = InStr(1, sBcvi, sRes)   '-- find .rsrc table.
         If (Pt1cvi = 0) Then   
              VIcvi.Init "", 2
              Set GetFileVersionInfo = VIcvi  'no resource table header found.
              Exit Function
         End If
     Pt1cvi = Pt1cvi + 12  '--  size of raw data is 4 bytes at offset of 16 into the .rsrc table. 
        A1cvi = GetArray(Mid(s1cvi, Pt1cvi, 12))  '-- get the same string as a numeric array to Read offset numbers.  
           For iOffSet = 0 to 3
                A4cvi(iOffSet) = A1cvi(iOffSet)
           Next
             VLocRes = GetNumFromBytes(A4cvi) 
           For iOffSet = 0 to 3
                A4cvi(iOffSet) = A1cvi(iOffSet + 4)
           Next
             SizeRes = GetNumFromBytes(A4cvi) '--size of resource section in bytes.
           For iOffSet = 0 to 3
                A4cvi(iOffSet) = A1cvi(iOffSet + 8)
           Next
              LocRes = GetNumFromBytes(A4cvi)    '-- offset location of resource section.  
          Pt1cvi = InStr(1, sBcvi, ".aspack")   '-- find .rsrc table.
             If (Pt1cvi > 0) Then
                  BooAspack = True
                      Pt1cvi = Pt1cvi + 12    '--  virtual offset is first 4 bytes; raw offset is bytes 9-12.
                      A1cvi = GetArray(Mid(s1cvi, Pt1cvi, 12))                      
                   For iOffSet = 0 to 3
                      A4cvi(iOffSet) = A1cvi(iOffSet)
                   Next
                     VLocAspack = GetNumFromBytes(A4cvi)             
                   For iOffSet = 0 to 3
                      A4cvi(iOffSet) = A1cvi(iOffSet + 8)
                   Next
                     LocAspack = GetNumFromBytes(A4cvi) 
              End If    
  
   Boocvi = False
   Set TScvi = FSOcvi.OpenTextFile(sFilePath, 1)
      TScvi.Skip LocRes + 12  '-- get number of names from bytes 13,14 in top level "Type" directory.
        s1cvi = TScvi.Read(2)       '-- Read bytes 13,14 to get number of named resource types.
          iNum1cvi = Asc(s1cvi)       '-- number of names.
        s1cvi = TScvi.Read(2)       '-- Read bytes 15,16 to get number of numbered resource types.
          iNum2cvi = Asc(s1cvi)       '-- number of nums.
        
       If (iNum2cvi = 0) Then '-- no numbered entries. have to quit here.
            TScvi.Close
            Set TScvi = Nothing
             VIcvi.Init "", 3
            Set GetFileVersionInfo = VIcvi  'failed to find version info in resource table.
            Exit Function
       End If
     
     If (iNum1cvi > 0) Then TScvi.Skip (iNum1cvi * 8) '-- Skip past named entries.
     iReadPt = LocRes + 16 + (iNum1cvi * 8)  '-- update file offset variable because this will be needed.
     Boocvi = False
        For iOffSet = 1 to iNum2cvi
           s1cvi = TScvi.Read(8)
           iReadPt = iReadPt + 8
              If (Asc(s1cvi) = 16) Then  '-- this is version info. entry.
                 Boocvi = True
                 Exit For
              End If
        Next
     If (Boocvi = False) Then  '-- have to quit. no version info. entry found.
         TScvi.Close
         Set TScvi = Nothing
          VIcvi.Init "", 3
        Set GetFileVersionInfo = VIcvi  'failed to find version info in resource table.
         Exit Function
     End If
       
     A1cvi = GetArray(s1cvi)  '-- get a byte array for version info entry at top level.
     iOffSet = 0
     iNum3cvi = 1
   Do
       For iNum1cvi = 0 to 2  '-- get offset number to next level from 2nd 4 bytes of entry structure.  
          A4cvi(iNum1cvi) = A1cvi(iNum1cvi + 4)
       Next
            A4cvi(3) = 0
            iNum2cvi = GetNumFromBytes(A4cvi)        
       If (A1cvi(7) > 127) Then  '-- high bit was set in entry offset value, so it's just a pointer to another pointer.    
             iNum2cvi = LocRes + iNum2cvi + 16
             TScvi.Skip (iNum2cvi - iReadPt)   '- 1)
             s1cvi = TScvi.Read(8)
             iReadPt = iReadPt + ((iNum2cvi - iReadPt) + 8)
             A1cvi = GetArray(s1cvi)
       Else  '-- this is the offset of version info offset info.! 
              iOffSet = (iNum2cvi + LocRes)
              Exit Do
       End If
          iNum3cvi = iNum3cvi + 1
          If (iNum3cvi > 10) Then Exit Do
   Loop    
       If (iOffSet = 0) Then  '-- have to quit. no final offset found.       
            TScvi.Close
            Set TScvi = Nothing
             VIcvi.Init "", 3
            Set GetFileVersionInfo = VIcvi  'failed to find version info in resource table.
            Exit Function
       End If
   TScvi.Skip (iOffSet - iReadPt) 
   s1cvi = TScvi.Read(8)
   iReadPt = iReadPt + ((iOffSet - iReadPt) + 8)
    A1cvi = GetArray(s1cvi)
       For iNum1cvi = 0 to 3
         A4cvi(iNum1cvi) = A1cvi(iNum1cvi)
       Next   
           VIOffset = GetNumFromBytes(A4cvi)  '--offset of version info. given in .rsrc section.
           ReadOffset = ((VIOffset - VLocRes) + LocRes)
       For iNum1cvi = 0 to 3
         A4cvi(iNum1cvi) = A1cvi(iNum1cvi + 4)
       Next      
           SizeRes = GetNumFromBytes(A4cvi)
    TScvi.Skip (ReadOffset - iReadPt)
    s1cvi = TScvi.Read(SizeRes)  '-- read out the entire FileVersionInfo data area.
    TScvi.Close
  Set TScvi = Nothing
      sBcvi = GetByteString(s1cvi, True) '-- snip unicode.
      Pt1cvi = InStr(1, sBcvi, sVerString)                                                           
           If (Pt1cvi > 0) Then        '-- "VS_VER" was found, so process the string and quit.
                VIcvi.Init sBcvi, 0
               Set GetFileVersionInfo = VIcvi  ' ok              
           ElseIf (BooAspack = True) Then   '-- if "VS_VER" was not found but there is an "aspack" section then try that.
              ReadOffset = ((VIOffset - VLocAspack) + LocAspack)  '-- calculate a new file version info data offset.           
                Set TScvi = FSOcvi.OpenTextFile(sFilePath, 1)  '-- The file was closed and is now re-opened here. Keeping the file
                   TScvi.Skip ReadOffset                            '-- open "just in case" wouldn't have helped because the file pointer
                     s1cvi = TScvi.Read(SizeRes)                     '-- for this read may be further back thean the pointer was when the file
                   TScvi.Close                                  '-- was closed. So rather than try to sort out the read point, the file is just
                Set TScvi = Nothing                        '-- opened fresh and Skip is used.
                   sBcvi = GetByteString(s1cvi, True) 
                   Pt1cvi = InStr(1, sBcvi, sVerString) 
                     If (Pt1cvi > 0) Then     
                         VIcvi.Init sBcvi, 0
                         Set GetFileVersionInfo = VIcvi  ' ok
                     Else  
                        VIcvi.Init "", 3
                        Set GetFileVersionInfo = VIcvi  'failed to find version info in resource table.
                     End If  
           Else   
                 VIcvi.Init "", 3
                 Set GetFileVersionInfo = VIcvi  'failed to find version info in resource table.         
           End If
End Function  
 
'-------------- simplified version of GetByteString For this Class. ---------------------
Private Function GetByteString(sStr, SnipUnicode)
  Dim sRet, iLen, iA, iLen2, A2cvi()
    On Error Resume Next
      iLen2 = 0
   If (SnipUnicode = False) Then
       ReDim A2cvi(len(sStr) - 1)
        For iLen = 1 to Len(sStr)
            iA = Asc(Mid(sStr, iLen, 1))
              If iA = 0 Then iA = 42  '-- converts 0-byte to *
            A2cvi(iLen - 1) = Chr(iA)
        Next
   Else     
      ReDim A2cvi((len(sStr) \ 2) - 1)
       For iLen = 1 to Len(sStr) step 2
             iA = Asc(Mid(sStr, iLen, 1))
                If iA = 0 Then iA = 42  '-- converts 0-byte to *
              A2cvi(iLen2) = Chr(iA)
              iLen2 = iLen2 + 1
       Next  
   End If     
       GetByteString = Join(A2cvi, "")
End Function
'-------------------------------- Simplified version of GetArray. -----------------------
Private Function GetArray(sStr)
Dim iA, Len1, Len2, AStr()
  On Error Resume Next
    Len1 = Len(sStr)
    ReDim AStr(Len1 - 1)
     For iA = 1 to Len1
        AStr(iA - 1) = Asc(Mid(sStr, iA, 1))
     Next      
         GetArray = AStr    
End Function
'-------------------- return a number from 2 or 4 bytes. ---------------
Private Function GetNumFromBytes(ABytes)
   Dim Num1
    Err.Clear
        On Error Resume Next
        GetNumFromBytes = -1
    Num1 = ABytes(0) + (ABytes(1) * 256)
      If (UBound(ABytes) = 3) Then
          Num1 = Num1 + (ABytes(2) * 65536) + (ABytes(3) * 16777216)
      End If
    If (Err.number = 0) Then GetNumFromBytes = Num1
End Function
  
End Class
 
'---------===================== Class SVInfo ========================--------------------------
 
'-- This class is returned by GetFileVersionInfo in other class. ------------------------
Class VSInfo
 Private sFVI, iErr, Char1
 
Private Sub Class_Initialize()
  Char1 = Chr(1)
End Sub
'-- class receives error code and, hopefully, version info data when this is called by GetFileVersionInfo.
Public Sub Init(sVInfo, iErrCode)
  sFVI = sVInfo
  iErr = iErrCode
End Sub
 
'-- State is error code. If State <> 0 then there's no version info.
'  Possible State values: 0 = success. 1 - invalid file path. 2 - no .rsrc table listed in section table. 
'  3 - failed to find version info. 4 - not a PE file. 5 - file is a 16-bit executable. ("NE" file rather than "PE")
Public Property Get State()
  State = iErr
End Property
 
  '----------------- parse version info string to get specific value.
  '-- call like: s = Cls.Value("Company")
  '-- possible values: CompanyName FileDescription FileVersion InternalName LegalCopyright OriginalFilename ProductName ProductVersion
Public Property Get Value(sValName)
  On Error Resume Next
    Value = ""
      If iErr <> 0 Then Exit Property
    Value = GetInfo(sValName) 
End Property
 
Private Function GetInfo(sVal)
  Dim Pta, Ptb, LenVal, s4
       On Error Resume Next
         GetInfo = ""
    LenVal = Len(sVal) + 1  '-- length of info string: "CompanyName" = 11
     Pta = InStr(1, sFVI, sVal, 1)  '-- find string name.
       If (Pta > 0) Then
          Pta = Pta + LenVal
          Ptb = InStr((Pta + 1), sFVI, "*")   '-- look for next *. some properties are Name**value** and some are
            If Ptb > (Pta + 2) Then              '-- Name*value**. So start looking at 3rd character after. If that                      
               s4 = Mid(sFVI, Pta, (Ptb - Pta))    '-- character is * then it's Name*** which means there's
               s4 = Replace(s4, "*", "")                                           '--no value for that specific property.
              If InStr(1, s4, Char1, 0) = 0 Then GetInfo = s4   '-- check for Chr(1) which seems to be found
           End If                             ' between values. If it's in the string that means there is no value for
                                               ' this property and function has actually read next property name.
      End If 
End Function
 
End Class
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now