Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for getproductversion.vbs

(view source code of getproductversion.vbs as plain text)

  1. Option Explicit
  2. Dim objFSO, strExt, strFile
  3.  
  4. If WScript.Arguments.Named.Count > 0 Then Syntax
  5.  
  6. Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  7.  
  8. With WScript.Arguments.Unnamed
  9. 	If .Count = 1 Then
  10. 		strFile = objFSO.GetAbsolutePathName( .Item(0) )
  11. 		If objFSO.FileExists( strFile ) Then
  12. 			strExt = LCase( objFSO.GetExtensionName( strFile ) )
  13. 			If strExt = "hta" Then
  14. 				WScript.Echo GetHTAVersion( strFile )
  15. 			ElseIf strExt = "msi" Then
  16. 				WScript.Echo GetMSIProductVersion( strFile )
  17. 			Else
  18. 				WScript.Echo GetProductVersion( strFile )
  19. 			End If
  20. 		Else
  21. 			Syntax
  22. 		End If
  23. 	Else
  24. 		Syntax
  25. 	End If
  26. End With
  27.  
  28. Set objFSO = Nothing
  29.  
  30.  
  31. Function GetHTAVersion( myHTA )
  32. 	Dim objHTA, objMatch, objMatches, objRE, strHTA, strVer
  33. 	strVer = ""
  34. 	Set objHTA = objFSO.OpenTextFile( myHTA )
  35. 	strHTA = objHTA.ReadAll( )
  36. 	objHTA.Close
  37. 	Set objHTA = Nothing
  38. 	Set objRE  = New RegExp
  39. 	objRE.Global     = True
  40. 	objRE.IgnoreCase = True
  41. 	objRE.Pattern    = "<HTA:APPLICATION[^>]+VERSION=""([^""]*)""[^>]*/>"
  42. 	If objRE.Test( strHTA ) Then
  43. 		Set objMatches = objRE.Execute( strHTA )
  44. 		If objMatches.Count > 0 Then
  45. 			If objMatches(0).Submatches.Count > 0 Then
  46. 				GetHTAVersion = objMatches.Item(0).Submatches(0)
  47. 			End If
  48. 		End If
  49. 		Set objMatches = Nothing
  50. 	End If
  51. 	Set objRE = Nothing
  52. End Function
  53.  
  54.  
  55. Function GetMSIProductVersion( myFile )
  56. 	' Code by Arnout Grootveld
  57. 	' http://stackoverflow.com/a/328710
  58. 	Const msiOpenDatabaseModeReadOnly = 0
  59. 	Dim objMSI, objDB, objView, strVersion
  60. 	GetMSIProductVersion = ""
  61. 	Set objMSI  = CreateObject( "WindowsInstaller.Installer" )
  62. 	Set objDB   = objMSI.OpenDataBase( myFile, msiOpenDatabaseModeReadOnly )
  63. 	Set objView = objDB.OpenView( "SELECT `Value` FROM `Property` WHERE `Property` = 'ProductVersion'" )
  64. 	Call objView.Execute( )
  65. 	strVersion = objView.Fetch( ).StringData(1)
  66. 	' Replace commas by dots
  67. 	strVersion = Replace( strVersion, ",", "." )
  68. 	' Remove spaces
  69. 	strVersion = Replace( strVersion, " ", ""  )
  70. 	GetMSIProductVersion = strVersion
  71. End Function
  72.  
  73.  
  74. Function GetProductVersion( myFile )
  75. 	' Based on code by Maputi on StackOverflow.com:
  76. 	' http://stackoverflow.com/a/2990698
  77. 	Dim arrTranslations
  78. 	Dim i
  79. 	Dim objFolder, objFolderItem, objShell
  80. 	Dim strFileName, strPropertyName, strParentFolder, strVersion
  81. 	' Note that property names are language dependent, so you may have to add the lower case property name for your own language
  82. 	Set arrTranslations = CreateObject( "System.Collections.ArrayList" )
  83. 	arrTranslations.Add "product version" ' English
  84. 	arrTranslations.Add "productversie"   ' Dutch
  85. 	strVersion = ""
  86. 	strFileName       = objFSO.GetFileName( myFile )
  87. 	strParentFolder   = objFSO.GetParentFolderName( myFile )
  88. 	Set objShell      = CreateObject( "Shell.Application" )
  89. 	Set objFolder     = objShell.Namespace( strParentFolder )
  90. 	Set objFolderItem = objFolder.ParseName( strFileName )
  91. 	For i = 0 To 300
  92. 		strPropertyName = objFolder.GetDetailsOf( objFolder.Items, i )
  93. 		If arrTranslations.Contains( LCase( strPropertyName ) ) Then
  94. 			' Product Version
  95. 			strVersion = objFolder.GetDetailsOf( objFolderItem, i )
  96. 			If strVersion = "" Then
  97. 				' File Version
  98. 				strVersion = objFSO.GetFileVersion( myFile )
  99. 			End If
  100. 			Exit For
  101. 		End If
  102. 	Next
  103. 	Set objFolderItem   = Nothing
  104. 	Set objFolder       = Nothing
  105. 	Set objShell        = Nothing
  106. 	Set arrTranslations = Nothing
  107. 	' Replace commas by dots
  108. 	strVersion = Replace( strVersion, ",", "." )
  109. 	' Remove spaces
  110. 	strVersion = Replace( strVersion, " ", ""  )
  111. 	GetProductVersion = strVersion
  112. End Function
  113.  
  114.  
  115. Sub Syntax
  116. 	Dim strMsg
  117. 	strMsg = "GetProductVersion.vbs,  Version 1.02" _
  118. 	       & vbCrLf _
  119. 	       & "Return the product version for a specified file (dll, exe, hta or msi)" _
  120. 	       & vbCrLf & vbCrLf _
  121. 	       & "Usage:    CSCRIPT.EXE  //NoLogo  GetProductVersion.vbs  filename" _
  122. 	       & vbCrLf & vbCrLf _
  123. 	       & "Where:    ""filename""   is the file whose product version we want to determine" _
  124. 	       & vbCrLf & vbCrLf _
  125. 	       & "Credits:  General product version by Maputi on StackOverflow.com:" _
  126. 	       & vbCrLf _
  127. 	       & "          http://stackoverflow.com/a/2990698" _
  128. 	       & vbCrLf _
  129. 	       & "          MSI product version by Arnout Grootveld on StackOverflow.com:" _
  130. 	       & vbCrLf _
  131. 	       & "          http://stackoverflow.com/a/328710" _
  132. 	       & vbCrLf & vbCrLf _
  133. 	       & "Written by Rob van der Woude" _
  134. 	       & vbCrLf _
  135. 	       & "http://www.robvanderwoude.com"
  136. 	WScript.Echo strMsg
  137. 	WScript.Quit 1
  138. End Sub
  139.  

page last uploaded: 2016-12-15, 11:20