Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for wshver.vbs

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

  1. Option Explicit
  2.  
  3. Dim arrReq, arrVer, blnArg, i, intReq, intRet, strVer
  4.  
  5. intRet = 0
  6.  
  7. With WScript
  8. 	' Read the current version
  9. 	arrVer = Split( .Version, "." )
  10. 	ReDim Preserve arrVer(2)
  11. 	arrVer(2) = .BuildVersion
  12. 	strVer = .Version & "." & .BuildVersion
  13. 	' Either 1 unnamed argument or 1 named, not both
  14.  
  15. 	If .Arguments.Named.Count * .Arguments.Unnamed.Count > 0 Then Syntax
  16. 	Select Case .Arguments.Unnamed.Count
  17. 		Case 0
  18. 			' Do nothing
  19. 		Case 1
  20. 			' Split the requested And actual versions into arrays
  21. 			arrReq = Split( .Arguments.Unnamed(0), "." )
  22. 			' Check if the requested version is numeric
  23. 			For i = 0 To UBound( arrReq )
  24. 				If Not IsNumeric( arrReq(i) ) Then Syntax
  25. 			Next
  26. 			' Compare the requested and actual versions
  27. 			intRet = 0
  28. 			For i = 0 To Min( 2, UBound( arrReq ) )
  29. 				If CLng( arrVer(i) ) > CLng( arrReq(i) ) Then
  30. 					Exit For
  31. 				End If
  32. 				If CLng( arrVer(i) ) < CLng( arrReq(i) ) Then
  33. 					intRet = 1
  34. 					Exit For
  35. 				End If
  36. 			Next
  37. 			strVer = .Version & "." & .BuildVersion
  38. 			If strVer = .Arguments.Unnamed(0) Then intRet = 0
  39. 		Case Else
  40. 			Syntax
  41. 	End Select
  42.  
  43. 	Select Case .Arguments.Named.Count
  44. 		Case 0
  45. 			' No switch: just display full version
  46. 		Case 1
  47. 			blnArg = False
  48. 			If .Arguments.Named.Exists( "Major" ) Then
  49. 				strVer = arrVer(0)
  50. 				intRet = Compare( strVer, .Arguments.Named( "Major" ) )
  51. 				blnArg = True
  52. 			End If
  53. 			If .Arguments.Named.Exists( "Minor" ) Then
  54. 				strVer = arrVer(1)
  55. 				intRet = Compare( strVer, .Arguments.Named( "Minor" ) )
  56. 				blnArg = True
  57. 			End If
  58. 			If .Arguments.Named.Exists( "Build" ) Then
  59. 				strVer = arrVer(2)
  60. 				intRet = Compare( strVer, .Arguments.Named( "Build" ) )
  61. 				blnArg = True
  62. 			End If
  63. 			If Not blnArg Then Syntax
  64. 		Case Else
  65. 			Syntax
  66. 	End Select
  67. 	' Display the version or part of it
  68. 	.Echo strVer
  69. End With
  70.  
  71. ' Exit with the calculated return code
  72. WScript.Quit intRet
  73.  
  74.  
  75. Function Compare( myVer, myReq )
  76. 	If MyReq = "" Then
  77. 		Compare = myVer
  78. 	Else
  79. 		If IsNumeric( myReq ) Then
  80. 			If myVer < myReq Then
  81. 				Compare = 1
  82. 			Else
  83. 				Compare = 0
  84. 			End If
  85. 		Else
  86. 			Syntax
  87. 		End If
  88. 	End If
  89. End Function
  90.  
  91.  
  92. Function Min( num1, num2 )
  93. 	If Not IsNumeric( num1 ) Then Syntax
  94. 	If Not IsNumeric( num2 ) Then Syntax
  95. 	If num1 < num2 Then
  96. 		Min = num1
  97. 	Else
  98. 		Min = num2
  99. 	End If
  100. End Function
  101.  
  102.  
  103. Sub Syntax
  104. 	Dim strMsg, strVer
  105. 	strVer = arrVer(0) & "." & arrVer(1) & "." & arrVer(2)
  106. 	strMsg = "WSHVer.vbs,  Version 1.10" _
  107. 	       & vbCrLf _
  108. 	       & "Return WSH version number or part of it" _
  109. 	       & vbCrLf & vbCrLf _
  110. 	       & "Usage:  WSHVER.VBS [ minreq | (/Major|/Minor|/Build)[:minreq] ]" _
  111. 	       & vbCrLf & vbCrLf _
  112. 	       & "Where:  /Major  displays the major version number (e.g. " & arrver(0) & ")" _
  113. 	       & vbCrLf _
  114. 	       & "        /Minor  displays the minor version number (e.g. " & arrver(1) & ")" _
  115. 	       & vbCrLf _
  116. 	       & "        /Build  displays the build number         (e.g. " & arrver(2) & ")" _
  117. 	       & vbCrLf _
  118. 	       & "        minreq  is the minimum required value; if specified, the return code" _
  119. 	       & vbCrLf _
  120. 	       & "                will be 0 if the actual version is equal to or greater than" _
  121. 	       & vbCrLf _
  122. 	       & "                Req, or 1 if it is less; if not specified, the return code" _
  123. 	       & vbCrLf _
  124. 	       & "                will be equal to the version number part (e.g. " & arrver(1) & " for /Minor)" _
  125. 	       & vbCrLf & vbCrLf _
  126. 	       & "Examples                 Display      Return code" _
  127. 	       & vbCrLf _
  128. 	       & "====================     =========    ===========================" _
  129. 	       & vbCrLf _
  130. 	       & "WSHVER.VBS " & arrver(0) & "." & arrVer(1) - 1 & "           " & strVer & "        0 (requirement met)" _
  131. 	       & vbCrLf _
  132. 	       & "WSHVER.VBS " & arrVer(0) & "." & arrVer(1) - 1 & "." & arrVer(2) + 10000 & "     " & strver & "        0 (requirement met)" _
  133. 	       & vbCrLf _
  134. 	       & "WSHVER.VBS " & arrver(0) + 1 & "             " & strver & "        1 (requirement not met)" _
  135. 	       & vbCrLf _
  136. 	       & "WSHVER.VBS /Major        " & arrver(0) & "                " & arrver(0) & " (major version)" _
  137. 	       & vbCrLf _
  138. 	       & "WSHVER.VBS /Minor:" & arrver(1) - 1 & "      " & arrver(1) & "                0 (requirement met)" _
  139. 	       & vbCrLf _
  140. 	       & "WSHVER.VBS /Minor        " & arrver(1) & "                " & arrver(1) & " (minor version)" _
  141. 	       & vbCrLf _
  142. 	       & "WSHVER.VBS /Build        " & arrver(2) & "        " & arrver(2) & " (build version)" _
  143. 	       & vbCrLf & vbCrLf _
  144. 	       & "Written by Rob van der Woude" _
  145. 	       & vbCrLf _
  146. 	       & "http://www.robvanderwoude.com"
  147. 	WScript.Echo strMsg
  148. 	WScript.Quit 1
  149. End Sub
  150.  

page last modified: 2024-04-16; loaded in 0.0068 seconds