Rob van der Woude's Scripting Pages
Powered by GeSHi

VBScript Scripting Techniques > Working with Files > File Versions

File Versions

 

 

Shell.Application: ProductVersion
Return a file's product version
VBScript Code: Download 💾
  1. Function GetProductVersion( myFile )
  2. 	' Based on code by Maputi on StackOverflow.com:
  3. 	' http://stackoverflow.com/a/2990698
  4. 	Dim arrTranslations
  5. 	Dim i
  6. 	Dim objFolder, objFolderItem, objShell
  7. 	Dim strFileName, strPropertyName, strParentFolder, strVersion
  8. 	' Note that property names are language dependent, so you may have to add the lower case property name for your own language
  9. 	Set arrTranslations = CreateObject( "System.Collections.ArrayList" )
  10. 	arrTranslations.Add "product version" ' English
  11. 	arrTranslations.Add "productversie"   ' Dutch
  12. 	strVersion = ""
  13. 	strFileName       = objFSO.GetFileName( myFile )
  14. 	strParentFolder   = objFSO.GetParentFolderName( myFile )
  15. 	Set objShell      = CreateObject( "Shell.Application" )
  16. 	Set objFolder     = objShell.Namespace( strParentFolder )
  17. 	Set objFolderItem = objFolder.ParseName( strFileName )
  18. 	For i = 0 To 300
  19. 		strPropertyName = objFolder.GetDetailsOf( objFolder.Items, i )
  20. 		If arrTranslations.Contains( LCase( strPropertyName ) ) Then
  21. 			' Product Version
  22. 			strVersion = objFolder.GetDetailsOf( objFolderItem, i )
  23. 			Exit For
  24. 		End If
  25. 	Next
  26. 	Set objFolderItem   = Nothing
  27. 	Set objFolder       = Nothing
  28. 	Set objShell        = Nothing
  29. 	Set arrTranslations = Nothing
  30. 	' Replace commas by dots
  31. 	strVersion = Replace( strVersion, ",", "." )
  32. 	' Remove spaces
  33. 	strVersion = Replace( strVersion, " ", ""  )
  34. 	GetProductVersion = strVersion
  35. End Function
 
Requirements:
Windows version: 2000 and later with .NET Framework 2.0 or later
Network: any
Script Engine: any
Summarized: Works in Windows 2000 and later versions with .NET Framework 2.0 or later installed;
works with all scripting engines;
may require the addition of a translation for "Product Version" in languages other than English and Dutch.
 
[Back to the top of this page]

 

WindowsInstaller.Installer: ProductVersion
Rerurn an MSI file's product version
VBScript Code:  
  1. Function GetMSIProductVersion( myFile )
  2. 	' Code by Arnout Grootveld
  3. 	' http://stackoverflow.com/a/328710
  4. 	Const msiOpenDatabaseModeReadOnly = 0
  5. 	Dim objMSI, objDB, objView, strVersion
  6. 	GetMSIProductVersion = ""
  7. 	Set objMSI  = CreateObject( "WindowsInstaller.Installer" )
  8. 	Set objDB   = objMSI.OpenDataBase( myFile, msiOpenDatabaseModeReadOnly )
  9. 	Set objView = objDB.OpenView( "SELECT `Value` FROM `Property` WHERE `Property` = 'ProductVersion'" )
  10. 	Call objView.Execute( )
  11. 	strVersion = objView.Fetch( ).StringData(1)
  12. 	' Replace commas by dots
  13. 	strVersion = Replace( strVersion, ",", "." )
  14. 	' Remove spaces
  15. 	strVersion = Replace( strVersion, " ", ""  )
  16. 	GetMSIProductVersion = strVersion
  17. End Function
 
Requirements:
Windows version: Windows XP or later
Network: any
Script Engine: any
Summarized: Works in Windows XP and later versions with all scripting engines.
 
[Back to the top of this page]

 

Scripting.FileSystemObject: GetFileVersion
Rerurn a file's file version
VBScript Code:  
  1. Dim objFSO, strFileName
  2. strFileName = "C:\Windows\notepad.exe"
  3. Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  4. WScript.Echo objFSO.GetFileVersion( strFileName )
  5. Set objFSO = Nothing
 
Requirements:
Windows version: any
Network: any
Script Engine: any
Summarized: Works in all Windows versions with all scripting engines.
 
[Back to the top of this page]

 

Scripting.FileSystemObject: DateLastModified
Rerurn a file's timestamp
VBScript Code:  
  1. Dim objFSO, strFileName
  2. strFileName = "C:\Windows\notepad.exe"
  3. Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  4. WScript.Echo objFSO.GetFile( strFileName ).DateLastModified
  5. Set objFSO = Nothing
 
Requirements:
Windows version: any
Network: any
Script Engine: any
Summarized: Works in all Windows versions with all scripting engines.
 
[Back to the top of this page]

 

CompareVersions
Compare two version strings digit by digit
VBScript Code:  
  1. Function CompareVersions( myVer1, myVer2 )
  2. 	' Function : CompareVersions compares 2 version strings and returns
  3. 	'            an integer value telling you which version is higher.
  4. 	' Returns  : A positive value means myVer1 is higher than myVer2,
  5. 	'            a negative value means myVer1 is lower than myVer2,
  6. 	'            zero means the versions are equal.
  7. 	'            If the absolute value of the result equals 2, only the
  8. 	'            number of digits from the shortes of versions were compared;
  9. 	'            if the absolute value equals 1, the shortest of versions had a
  10. 	'            number of zeroes appended before the comparison was conclusive.
  11. 	' Examples : CompareVersions( "10.2.0", "10.1.1900" ) returns  2
  12. 	'            CompareVersions( "10.1.0.1900", "10.1" ) returns  1
  13. 	'            CompareVersions( "10.00.0.0.0", "10.0" ) returns  0
  14. 	'            CompareVersions( "10.0", "10.0.0.1850" ) returns -1
  15. 	'            CompareVersions( "9.9.99.9999", "10.1" ) returns -2
  16. 	' Version   : 1.00 (2017-02-17)
  17. 	' Author    : Rob van der Woude
  18. 	'             http://www.robvanderwoude.com/
  19. 	'
  20. 	Dim arrVer1, arrVer2
  21. 	Dim i, intCompare, intMax, intMin
  22. 	intCompare = 0
  23. 	If myVer1 <> myVer2 Then
  24. 		arrVer1 = Split( myVer1, "." )
  25. 		arrVer2 = Split( myVer2, "." )
  26. 		If UBound( arrVer1 ) > UBound( arrVer2 ) Then
  27. 			intMax = UBound( arrVer1 )
  28. 			intMin = UBound( arrVer2 )
  29. 			For i = intMin To intMax
  30. 				myVer2 = myVer2 & ".0"
  31. 			Next
  32. 			arrVer2 = Split( myVer2, "." )
  33. 		ElseIf UBound( arrVer1 ) < UBound( arrVer2 ) Then
  34. 			intMax = UBound( arrVer2 )
  35. 			intMin = UBound( arrVer1 )
  36. 			For i = intMin To intMax
  37. 				myVer1 = myVer1 & ".0"
  38. 			Next
  39. 			arrVer1 = Split( myVer1, "." )
  40. 		Else
  41. 			intMax = UBound( arrVer1 )
  42. 			intMin = intMax
  43. 		End If
  44. 		For i = 0 To intMin
  45. 			If CInt( arrVer1(i) ) > CInt( arrVer2(i) ) Then
  46. 				intCompare = 2
  47. 				Exit For
  48. 			ElseIf CInt( arrVer1(i) ) < CInt( arrVer2(i) ) Then
  49. 				intCompare = -2
  50. 				Exit For
  51. 			End If
  52. 		Next
  53. 		If intCompare = 0 Then
  54. 			For i = intMin + 1 To intMax
  55. 				If CInt( arrVer1(i) ) > CInt( arrVer2(i) ) Then
  56. 					intCompare = 1
  57. 					Exit For
  58. 				ElseIf CInt( arrVer1(i) ) < CInt( arrVer2(i) ) Then
  59. 					intCompare = -1
  60. 					Exit For
  61. 				End If
  62. 			Next
  63. 		End If
  64. 	End If
  65. 	CompareVersions = intCompare
  66. End Function
 
Requirements:
Windows version: any
Network: any
Script Engine: any
Summarized: Works in all Windows versions with all scripting engines.
 
[Back to the top of this page]

page last modified: 2022-10-26; loaded in 0.0063 seconds